Power Automate - Trigger a Flow from multiple Libraries and sites
Introduction
In this blog post, we’ll explore how to trigger a single Power Automate flow from multiple SharePoint libraries and sites. This approach is particularly useful for scenarios where you need to standardize workflows across different document libraries and sites. It will avoid the need replicating the flows multiple times which may be harder to maintain and deploy.
The For a selected file
action is used to achieve this. I have tried the Manually trigger a flow
in vain as the context (site URL, library ID/name, and item ID) is not passed to the flow. This makes it difficult to determine the source of the trigger.
The Solution
The trick is to use the For a selected file
action. You can pick any SharePoint site and library to set up the trigger.
Step-by-Step Guide
1. Trigger For a selected file
A flow is triggered from the executeFlow
action using column formatting within a library. Here is an example of the JSON configuration:
Update the actionParams
flow ID to correspond with your specific flow. This format consists of the environment ID followed by the flow ID, as it pertains to a flow within a solution in a managed environment.
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "button",
"txtContent": "Set to Approved",
"customRowAction": {
"action": "executeFlow",
"actionParams": "{\"id\":\"v1/26763ce5-0000-0000-0000-d37e8bf80aaa/3156f74d-cdd8-42fd-bcd5-454fd521b961\", \"headerText\":\"Please enter the Last Approval Details\",\"runFlowButtonText\":\"Set to Approved\"}"
},
"attributes": {
"class": "ms-fontColor-themePrimary ms-fontColor-themeDarker--hover"
},
"style": {
"border": "none",
"background-color": "transparent",
"cursor": "pointer",
"text-align": "left"
},
"children": [
{
"elmType": "span",
"attributes": {
"iconName": "CheckMark"
},
"style": {
"padding-right": "8px"
}
},
{
"elmType": "span",
"txtContent": "Set to Approved"
}
]
}
When the button is clicked, the triggerBody will have the following format:
- For a PDF file
"body": {
"entity": {
"ID": 3,
"itemUrl": "https://contoso.sharepoint.com/teams/d-team-playground/ApprovedControlledDocuments/Forms/AllItems.aspx?id=%2Fteams%2Fd%2Dteam%2Dplayground%2FApprovedControlledDocuments%2FDocument1%2Epdf&parent=%2Fteams%2Fd%2Dteam%2Dplayground%2FApprovedControlledDocuments",
"fileName": "Document1.pdf",
"FileId": "3"
}
}
For an office file
"body": {
"entity": {
"ID": 1,
"itemUrl": "https://contoso.sharepoint.com/teams/D-TEAM-Site/_layouts/15/Doc.aspx?sourcedoc=%7Bd99c035a-49e3-49ec-a4a4-05f2d1f2817f%7D&action=edit&uid=%7BD99C035A-49E3-49EC-A4A4-05F2D1F2817F%7D&ListItemId=1&ListId=%7B70D252A6-9C4F-42E5-BED5-B2A126D0C5CE%7D&odsp=1&env=prod",
"fileName": "Document.docx",
"FileId": "1"
}
}
2. Extract Site URL and Library ID
Next steps is to work out the siteurl and libraryId from the itemUrl. This process differs slightly for PDF files and Office files. In this example, we’ll focus on office files.
using itemUrl in the format https://contoso.sharepoint.com/teams/D-TEAM-Site/_layouts/15/Doc.aspx?sourcedoc=%7Bd99c035a-49e3-49ec-a4a4-05f2d1f2817f%7D&action=edit&uid=%7BD99C035A-49E3-49EC-A4A4-05F2D1F2817F%7D&ListItemId=1&ListId=%7B70D252A6-9C4F-42E5-BED5-B2A126D0C5CE%7D&odsp=1&env=prod
to work out the siteurl and libraryId
site url
Add an ‘Initialize variable` action for siteUrl and use the following expression to retrieve it.
join(take(split(triggerBody()?['entity']?['itemUrl'], '/'), 5), '/')
Explanation:
split(triggerBody()?[’entity’]?[‘itemUrl’],’/’): The split function splits the itemUrl string into an array of substrings using the / character as the delimiter.
take(split(triggerBody()?[’entity’]?[‘itemUrl’],’/’), 5): The take function takes the first 5 elements from the array created by the split function.
join(take(split(triggerBody()?[’entity’]?[‘itemUrl’],’/’), 5),’/’): The join function joins the first 5 elements of the array back into a single string, using / as the separator.
The output of the function is https://contoso.sharepoint.com/teams/D-TEAM-Site
LibraryId
Add a ‘Initialize variable` action for libraryId and use the following expression to retrieve the libraryId.
replace(replace(split(split(decodeUriComponent(triggerBody()?['entity']?['itemUrl']), 'ListId=')[1], '&')[0],'}',''),'{','')
The output of the function is D99C035A-49E3-49EC-A4A4-05F2D1F2817F
3. Use Variables in Subsequent Actions
You can now use these variables in subsequent actions,e.g. to get file properties or perform other operations.
4. Change Run-Only Permissions
Amend the run-only permissions of the connections to run under a service account if using premium actions. This avoids requiring end users to create their own connections to run the flow.
Limitations
- Contextually the flow will appear only on the specified SharePoint site and library and won’t appear in other libraries.
The itemUrl format is different for folders and non office files, like PDF , hence ensure the expressions to retrieve libraryid, siteUrl are amended to reflect those differences.
The method specified will work for ‘for a selected file’ trigger only
Thoughts
- Trigger based on contentype rather than location would have been great but is not possible.
Trigr
custom product by Encodian, read Deploy a Power Automate Flow to multiple SharePoint libraries or lists for more info.- Considered One Flow to handle them all - how to subscribe to multiple SharePoint lists with one Flow but required some testing as Proof of concept hence gave up on the idea.
- Considered
When an HTTP request is received
trigger but it could not be triggered using executeflow function within column formatting. It would have required SPFx extension to trigger it.
Conclusion
By using the For a selected file
action and the provided expressions, you can effectively trigger a single Power Automate flow from multiple SharePoint libraries and sites. This approach simplifies workflow management and ensures consistency across your SharePoint environment.