In this article, we will learn how to add an action button on the list items view using column formatting which will trigger an MS Flow based on some conditions. This article will cover the below use cases.
- Usage of column formatting to add an action button.
- Trigger MS flow from list item view's action button.
- MS flow to get user input before MS flow starts.
The first thing we will do is to setup MS Flow for our example. We won't go into details of how to create a Microsoft Flow as there are a lot of articles available to cover this topic. Some of them are mentioned below.
MS Flow Setup - Step 1
I have created an MS Flow with trigger point as 'For selected items'. Below is the configuration of the triggering point. We have added two optional input fields using the 'Add input' button as marked below in the screenshot.
MS Flow Setup - Step 2
The next action item is to send mail, (you can ignore GetmyProfile action item from the above screenshot for this example). In this action Send Mail, we will send email to a particular user and add input forms entered by the user in the email body. Below is a screenshot for your reference.
MS Flow Setup - Step 3
Save MS Flow. The next item is to get the MS Flow ID which will be used later in the column formatting step. On the "Edit MS Flow" page, we will get MS Flow ID in URL. For example, for the above flow, below was the URL (obviously, changed GUIDs).
https://india.flow.microsoft.com/manage/environments/Default-5b9dueb0-1ek3-46psd-8da3-6ba895467b9/flows/2abcd334-z897-460a-92s8-461d43p97fd0
Text marked in red is your MS flow id. Note it down.
Now, let us set up our list to be used for this example.
Setup List - Step 1
- Create a List named 'CustomList'
- Add Status column as a choice with 'Pending', 'In progress' and 'Completed'
- Add StartFlow column as a calcuated column with formula ="". Here, we are setting up a dummy column as we will be using other fields to populate values in this field.
Setup List - Step 2
Next, we will use column formatting options to format our StartFlow column. Edit column again and paste the below JSON value in column formatting options.
Let us understand what are we doing here in JSON.
- We are using customRowAction with action executeFlow and passing actionParams to pass the id of MS flow(as noted in above step)
Note
Here, we are using Excel-based expression to check Status column value and passing id accordingly. For this example, I am passing the same id for all the cases. This to demonstrate we can trigger different flow based on fields values.
- Next thing we are doing here is based on Status columns value, formatting font colors with attributes.
Then, we are adding text and icon. Again, here we are using conditional formatting to display different icons and text based on the Status column's value.
- {
- "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
- "elmType": "button",
- "customRowAction": {
- "action": "executeFlow",
- "actionParams": "='{\"id\": \"' + if([$Status]=='Completed','2cabd779-a135-420a-90f8-461d56e83fd0',if([$Status]=='Inprogress','2cabd779-a135-420a-90f8-461d56e83fd0','2cabd779-a135-420a-90f8-461d56e83fd0')) + '\"}'"
- },
- "attributes": {
- "class": "='ms-fontColor-' + if([$Status]=='Inprogress','redDark', if([$Status]=='Completed','teal','orangeLight'))"
- },
- "style": {
- "border": "none",
- "background-color": "transparent",
- "cursor": "pointer"
- },
- "children": [
- {
- "elmType": "span",
- "attributes": {
- "iconName": "=if([$Status]=='Completed','Checkmark',if([$Status]=='Inprogress','ProgressLoopInner','Important'))"
- },
- "style": {
- "padding-right": "6px"
- }
- },
- {
- "elmType": "span",
- "txtContent": "=if([$Status]=='Completed','Thanks',if([$Status]=='Inprogress','Send Reminder','Send OverDue Mail'))"
- }
- ]
- }
Note
Other supported customRowAction values are,
- defaultClick - opens the information panel (same as clicking on an item)
- share - opens the sharing dialog for the item
- delete - opens the delete confirmation dialog for the item
- editProps - opens the item properties page in edit mode
- executeFlow - launches a specified flow for the item (the Flow ID is specified in the actionParams attribute)
Setup List - Step 3
- Add some dummy data to list with different values in status columns.
- Go to list view to check the output. Below is what you should see.
Testing - Step 1
Now let us see how to check what happens when the user clicks on the button. Go to list view (as in the above screenshot). Click on any list items action item. As our MS Flow takes user inputs, it will open the below window. Enter email id and custom message and click on Start Flow.
It should start flow, go to MS flow platform to check flow status.
https://india.flow.microsoft.com/manage/environments/Default-5b9dueb0-1ek3-46psd-8da3-6ba895467b9/flows/2abcd334-z897-460a-92s8-461d43p97fd0/ details
Testing - Step 2
Go to your email and check email.
In this article, we have seen how to add action button on list items view using column formatting which will trigger MS flow based on some conditions.