Introduction
In many cases, we want the flow to be triggered by an event, such as an item added or updated. However, sometimes we would like to trigger the flow programmatically from our SPFx web part. For example, trigger an email from the web part on a button click.
In this article, we will explore how we can trigger Power Automate (formerly MS Flow) workflow from SPFx web part.
Build Power automate workflow
We will start by building flow to send out an email. Later we will trigger it from the SPFx web part using the REST API call.
- Navigate to Power Automate in Office 365.
- From the left menu, click Create.
- Select “Instant flow”.
![Click on create]()
- In the modal dialog, specify the flow name (e.g. QuickMailSend).
- Select the trigger as “When an HTTP request is received”.
![When an HTTP request is received is received]()
- Click Create.
- The flow should get created as below.
![When a HTTP request is received]()
- We want to compose email with information about email address, subject, and body. Our request body will look like as below.
- Expand the activity “When a HTTP request is received”.
- Click “Generate from sample”.
![URL will be generated after save]()
- In the pop up, add the above request body JSON schema.
![Insert a sample JSON payload]()
- Click Done.
- The request body json schema will be generated.
![Generate from sample]()
- Click “+ New step”.
- Find an add an action “Send an email”.
![Choose an action]()
- Click on the field in the form, to add JSON parameters (from the schema).
![Dynamic content]()
- Click “+ New step”.
- Choose action “Response”.
![Response]()
- Click Save.
- Note down the HTTP POST URL.
![When a HTTP request is received]()
Test flow with soap UI
Download the SoapUI from here.
- Select Method as Post.
- Specify the endpoint as HTTP POST URL.
- Specify the header as Content-type with value application/JSON
- In the Body, specify the JSON.
![Try SoapUI pro]()
- Click Send.
- The email should be sent out to the specified email.
Develop SPFx web part to execute power automate flow
Open a command prompt. Create a directory for the SPFx solution.
Navigate to the above created directory.
Run the Yeoman SharePoint Generator to create the solution.
Yeoman generator will present you with the wizard by asking questions about the solution to be created.
![Welcome to the sharepoint client side]()
Once the scaffolding process is completed, lock down the version of the project dependencies by running below command.
In the command prompt type below command to open the solution in the code editor of your choice.
Code the web part
Open the web part file at “src\webparts\flowEmailer\FlowEmailerWebPart.ts”.
Add below imports.
Update render() method to include basic controls to input email address, subject, body, and button to send email.
Implement button click event to read user inputs and send an email.
Test the web part
- On the command prompt, type “gulp serve -nobrowser”.
- Open SharePoint site.
- Navigate to /_layouts/15/workbench.aspx
- Locate and add the web part (named FlowEmailer) to page.
![Wecome to sharePoint]()
- Provide the email, subject, and body.
- Verify the email from Outlook.
![New message]()
Summary
In this article, we explored the practical use case of triggering the Power Automate workflow from SPFx web part. The HTTP POST URL exposed by Power Automate flow can be triggered from SPFx web part using HttpClient.