{"id":1213,"date":"2017-02-01T17:51:40","date_gmt":"2017-02-02T00:51:40","guid":{"rendered":"https:\/\/toddbaginski.com\/blog\/?p=1213"},"modified":"2017-02-01T17:52:09","modified_gmt":"2017-02-02T00:52:09","slug":"how-to-run-a-microsoft-flow-from-a-sharepoint-framework-spfx-web-part","status":"publish","type":"post","link":"https:\/\/toddbaginski.com\/blog\/how-to-run-a-microsoft-flow-from-a-sharepoint-framework-spfx-web-part\/","title":{"rendered":"How To: Run a Microsoft Flow from a SharePoint Framework (SPFx) web part"},"content":{"rendered":"<p>In this post I&#8217;m going to show you how to call a Microsoft Flow from a SharePoint Framework (SPFx) web part. \u00a0In this sample scenario, the Microsoft Flow sends an email. \u00a0If you want to run this sample code first go to <a href=\"https:\/\/flow.microsoft.com\/en-us\/blog\/call-flow-restapi\/\" target=\"_blank\">this blog article<\/a> and follow the instructions to create a Microsoft Flow that sends email.<\/p>\n<p><strong>Note:<\/strong> \u00a0In the blog article I linked above the email address parameter is spelled <strong>emailAdress<\/strong>. \u00a0I renamed the parameter <strong>emailAddress<\/strong> when I created the Flow and the web part in this sample. \u00a0You will also need to rename the parameter to make it work with my sample web part code.<\/p>\n<p>This web part is super simple! \u00a0It is created with <strong>No javascript web framework <\/strong>option (you can see this below). \u00a0The goal of this web part is not to show you how to make web parts with React, Angular, Knockout, or JQuery. \u00a0<strong>The goal is to show you the bare bones implementation you can use to invoke a Microsoft Flow from an SPFx web part.<\/strong><\/p>\n<p><a href=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/01\/Create-web-part.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1221\" src=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/01\/Create-web-part.jpg\" alt=\"Create web part\" width=\"858\" height=\"700\" srcset=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/01\/Create-web-part.jpg 858w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/01\/Create-web-part-300x245.jpg 300w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/01\/Create-web-part-768x627.jpg 768w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/01\/Create-web-part-179x146.jpg 179w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/01\/Create-web-part-50x40.jpg 50w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/01\/Create-web-part-86x70.jpg 86w\" sizes=\"auto, (max-width:767px) 480px, (max-width:858px) 100vw, 858px\" \/><\/a><\/p>\n<p>You can find the web part code in this GitHub repository: <a href=\"https:\/\/github.com\/TBag\/SPFxFlowEmailWebPart\" target=\"_blank\">https:\/\/github.com\/TBag\/SPFxFlowEmailWebPart<\/a> \u00a0If you want to adapt this to use one of the frameworks mentioned above please feel free to do so and issue a Pull Request to the sample code GitHub repository. \u00a0I&#8217;m happy to add it to the repository to share with others.<\/p>\n<p><strong>Note:<\/strong>\u00a0 This web part was built on SPFx RC0.<\/p>\n<p>Here&#8217;s how to run it:<\/p>\n<p>Clone the code from the Git Hub repository:<\/p>\n<p><code>git clone\u00a0https:\/\/github.com\/TBag\/SPFxFlowEmailWebPart.git<\/code><\/p>\n<p>Install packages from npm:<\/p>\n<p><code>npm install<\/code><\/p>\n<p>Compile the web part and load the SharePoint workbench:<\/p>\n<p><code>gulp serve<\/code><\/p>\n<p>Add the web part to the SharePoint workbench:<\/p>\n<p><a href=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Add-to-workbench.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1236\" src=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Add-to-workbench.jpg\" alt=\"Add to workbench\" width=\"908\" height=\"545\" srcset=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Add-to-workbench.jpg 908w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Add-to-workbench-300x180.jpg 300w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Add-to-workbench-768x461.jpg 768w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Add-to-workbench-243x146.jpg 243w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Add-to-workbench-50x30.jpg 50w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Add-to-workbench-117x70.jpg 117w\" sizes=\"auto, (max-width:767px) 480px, (max-width:908px) 100vw, 908px\" \/><\/a><\/p>\n<p>Open the property pane to configure the web part.<\/p>\n<p>Enter the URL to the Flow you created. \u00a0To obtain the URL to invoke the Flow you made, open the Request in the Flow and click the <strong>copy button<\/strong> next to the <strong>HTTP POST to this URL<\/strong> textbox (shown below).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1234\" src=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Flow-Request.jpg\" alt=\"Flow - Request\" width=\"908\" height=\"1036\" srcset=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Flow-Request.jpg 908w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Flow-Request-263x300.jpg 263w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Flow-Request-768x876.jpg 768w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Flow-Request-897x1024.jpg 897w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Flow-Request-128x146.jpg 128w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Flow-Request-44x50.jpg 44w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Flow-Request-61x70.jpg 61w\" sizes=\"auto, (max-width:767px) 480px, (max-width:908px) 100vw, 908px\" \/><\/p>\n<p>Enter an <strong>email address<\/strong> to send the email to.<\/p>\n<p>Click <strong>Apply<\/strong>.<\/p>\n<p>Close the property pane.<\/p>\n<p>In the web part, click <strong>Send Email<\/strong>.<\/p>\n<p>If you open the Developer Tools (<strong>F12<\/strong>) you can view the status of the email in the console (shown below).<\/p>\n<p><a href=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Console.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1237\" src=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Console.jpg\" alt=\"Console\" width=\"681\" height=\"189\" srcset=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Console.jpg 681w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Console-300x83.jpg 300w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Console-260x72.jpg 260w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Console-50x14.jpg 50w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Console-200x56.jpg 200w\" sizes=\"auto, (max-width:767px) 480px, 681px\" \/><\/a><\/p>\n<p>Here&#8217;s what the email looks like. \u00a0Notice the Subject and Body match the values in the web part properties.<\/p>\n<p><a href=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Email.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1239\" src=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Email.jpg\" alt=\"Email\" width=\"1041\" height=\"532\" srcset=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Email.jpg 1041w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Email-300x153.jpg 300w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Email-768x392.jpg 768w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Email-1024x523.jpg 1024w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Email-260x133.jpg 260w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Email-50x26.jpg 50w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/Email-137x70.jpg 137w\" sizes=\"auto, (max-width:767px) 480px, (max-width:1041px) 100vw, 1041px\" \/><\/a><\/p>\n<p>OK, so <strong>how does it work<\/strong>? \u00a0Let&#8217;s take a look at the code real quick. \u00a0As I mentioned above, this is not some fancy web part built on any framework.<\/p>\n<p>Furthermore, this web part isn&#8217;t factored to the Nth degree to use different script files and modules. \u00a0If you want to see some awesome articles on how to factor your SPFx web parts to the Nth degree and use all sorts of frameworks I suggest you check out <a href=\"https:\/\/blog.mastykarz.nl\/\" target=\"_blank\">Waldek Mastykarz&#8217;s<\/a>, <a href=\"http:\/\/www.sharepointnutsandbolts.com\/\" target=\"_blank\">Chris O&#8217;Brien&#8217;s<\/a>, <a href=\"http:\/\/www.wictorwilen.se\/\" target=\"_blank\">Wictor Wil\u00e9n&#8217;s<\/a>, and <a href=\"http:\/\/andrewconnell.com\/\" target=\"_blank\">Andrew Connell&#8217;s<\/a> blogs. They all have a lot of great content about those topics.  You can also find good information on <a href=\"http:\/\/dev.office.com\" target=\"_blank\">http:\/\/dev.office.com<\/a>.<\/p>\n<p>I only modified 5 files to make this web part (shown below).<\/p>\n<p><a href=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/5-Files-Modified.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1245\" src=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/5-Files-Modified.jpg\" alt=\"5 Files Modified\" width=\"635\" height=\"883\" srcset=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/5-Files-Modified.jpg 635w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/5-Files-Modified-216x300.jpg 216w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/5-Files-Modified-105x146.jpg 105w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/5-Files-Modified-36x50.jpg 36w, https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/5-Files-Modified-50x70.jpg 50w\" sizes=\"auto, (max-width:767px) 480px, 635px\" \/><\/a><\/p>\n<p>Here&#8217;s what I added to each:<\/p>\n<ol>\n<li><strong>en-us.js<\/strong>\u00a0&#8211; modified strings and added additional strings that control the text in the web part property pane.<\/li>\n<li><strong>mystrings.d.ts &#8211;<\/strong>\u00a0added additional strings that control the text in the web part property pane.<\/li>\n<li><strong>SpFxFlowEmailWebPart.manifest.json<\/strong>\u00a0&#8211; added default values for some of the web part properties.<\/li>\n<li><strong>ISpFxFlowEmailWebPartProps.ts<\/strong>\u00a0&#8211; added the web part properties to the interface.<\/li>\n<li><strong>SpFxFlowEmailWebPart.ts<\/strong>\u00a0&#8211; added the controls for the web part properties to the property pane user interface.<\/li>\n<\/ol>\n<p>None of these changes are a big deal, so I&#8217;m not going to bother putting code snippets that show them in this blog post. \u00a0You can see the changes for yourself in the sample code.<\/p>\n<p><strong>OK, now for the fun part; calling the Flow from the SPFx web part!<\/strong><\/p>\n<p>All of this code is in the <strong>SpFxFlowEmailWebPart.ts<\/strong> file.<\/p>\n<p>Here&#8217;s the code that executes when the Send Email button is clicked in the web part. First, the code validates the FlowURL is present. If it is not, the out of the box <strong>context.<a href=\"https:\/\/dev.office.com\/sharepoint\/reference\/spfx\/sp-webpart-base\/iclientsidewebpartstatusrenderer\" target=\"_blank\">statusRenderer<\/a>.renderError<\/strong> SPFx method is used to display the error message.<\/p>\n<p>Then, the code validates the email address is valid by calling a simple helper function named <strong>validateEmail <\/strong>you will see in the web part code. This function uses a regular expression to validate the email. The out of the box <strong>context.<a href=\"https:\/\/dev.office.com\/sharepoint\/reference\/spfx\/sp-webpart-base\/iclientsidewebpartstatusrenderer\" target=\"_blank\">statusRenderer<\/a>.renderError<\/strong> SPFx method is again used to display the error message.<\/p>\n<p>Finally, the code invokes the <strong>sendEmailViaOffice365Outlook <\/strong>method and passes in the web part properties to send the email.<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/TBag\/665d6cc3146d1a3266a30243be6bfe6b.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/TBag\/665d6cc3146d1a3266a30243be6bfe6b\">Gist<\/a>.<\/noscript><\/div>\n<p>The <strong>sendEmailViaOffice365Outlook <\/strong>method does a few things to call the Flow. First it sets a constant equal to the flowURL web part property. Then, it assembles the body for the POST request. The body contains the JSON string containing the email address, subject, and body. Next, the request headers are set. In this case, the only request header that you need to pass to the Flow is the Content-type. Next, the <a href=\"https:\/\/dev.office.com\/sharepoint\/reference\/spfx\/sp-http\/ihttpclientoptions\" target=\"_blank\">IHttpClientOptions<\/a> are created. This class encapsulates the body and headers so they are sent properly via the SPFx <a href=\"https:\/\/dev.office.com\/sharepoint\/reference\/spfx\/sp-http\/httpclient\" target=\"_blank\">httpClient<\/a> with the POST request. Finally, the post method is invoked on the SPFx <a href=\"https:\/\/dev.office.com\/sharepoint\/reference\/spfx\/sp-http\/httpclient\" target=\"_blank\">httpClient<\/a>. This method takes in the Flow URL to POST to, the <a href=\"https:\/\/dev.office.com\/sharepoint\/reference\/spfx\/sp-http\/ihttpclientconfiguration\" target=\"_blank\">IHttpClientConfigurations<\/a>, the <a href=\"https:\/\/dev.office.com\/sharepoint\/reference\/spfx\/sp-http\/ihttpclientoptions\" target=\"_blank\">IHttpClientOptions<\/a> and ultimately makes the POST request to the Flow.<\/p>\n<p><strong>Note:<\/strong> I suggest you read <a href=\"http:\/\/www.sharepointnutsandbolts.com\/2016\/08\/async-rest-calls-promises-httpclient-jquery.html\">this article<\/a> by Chris O&#8217;Brien to learn more about the SPFx <a href=\"https:\/\/dev.office.com\/sharepoint\/reference\/spfx\/sp-http\/httpclient\" target=\"_blank\">httpClient<\/a>\u00a0class.<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/TBag\/866bfa2de1aa469b72f08d88b97bdfc9.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/TBag\/866bfa2de1aa469b72f08d88b97bdfc9\">Gist<\/a>.<\/noscript><\/div>\n<p>Here&#8217;s the web part in action.  Click Send Email and the email is sent immediately.  Nice performance!<\/p>\n<p><a href=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/SPFxFlowEmailWebPart.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2017\/02\/SPFxFlowEmailWebPart.gif\" alt=\"SPFx Flow Email Web Part\" width=\"1002\" height=\"858\" class=\"alignnone size-full wp-image-1263\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I&#8217;m going to show you how to call a Microsoft Flow from a SharePoint Framework (SPFx) web part. \u00a0In this sample scenario, the<span class=\"excerpt-hellip\"> [\u2026]<\/span><\/p>\n","protected":false},"author":1,"featured_media":1253,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[101,103,104,97,106],"tags":[42,10,113,5,6,45,7,43,115],"class_list":["post-1213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure","category-cloud","category-office-365","category-sharepoint","category-sharepoint-2016","tag-cloud","tag-custom-development","tag-flow","tag-helpful-code","tag-how-to-sharepoint","tag-o365","tag-sharepoint","tag-sharepoint-online","tag-spfx"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts\/1213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/comments?post=1213"}],"version-history":[{"count":29,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts\/1213\/revisions"}],"predecessor-version":[{"id":1266,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts\/1213\/revisions\/1266"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/media\/1253"}],"wp:attachment":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/media?parent=1213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/categories?post=1213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/tags?post=1213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}