Power Automate - Trigger a Flow from multiple Libraries/lists and sites
Introduction
In this blog post, we’ll explore how to trigger a single Power Automate flow from multiple SharePoint libraries, lists, and sites. This approach is particularly useful for standardizing workflows across different document libraries and sites, avoiding the need to replicate flows multiple times. This not only simplifies maintenance but also streamlines deployment.
The For a selected file
or For a selected item
trigger action is key to achieving this. While the Manually trigger a flow
action was considered, it does not pass the necessary context (site URL, library ID/name, and item ID) to the flow, making it unsuitable for this scenario.
The Solution
The solution involves using the For a selected file
or For a selected item
action. You can configure the trigger by selecting any SharePoint site and library.
For a selected file Trigger
For a selected item Trigger
Step-by-Step Guide
1. Configure the For a selected file
or For a selected item
Trigger
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"
}
}
For a SharePoint List Item
"body": {
"text_1": "3",
"date": "2025-05-13",
"entity": {
"ID": 1,
"itemUrl": "https://{siteurl}/_layouts/15/listform.aspx?PageType=4&ListId=%7B9AA7C048-D3D8-4C75-934A-D1C491432CE5%7D&Source=https%3A%2F%2F{siteurl}2%2FLists%2F{listurl}%2FAllItems.aspx&RootFolder=%2F{siteUrl}2%2F{listurl}&ID=1&ContentTypeId=0x0100B0E7B25049CA144199BDE02D715D0EC9001017085E671B4E46A1ABEA6141816A6D",
"fileName": "Email Comms",
"FileId": "1"
}
2. Extract Site URL and Library/List ID
Next steps is to work out the siteurl and libraryId/ listid from the itemUrl. This process differs slightly for PDF files and Office files/SharePoint list items. In this example, we’ll focus on office files/SharePoint list items.
using itemUrl.
The itemUrl for a file is: 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
or https://{siteurl}/_layouts/15/listform.aspx?PageType=4&ListId=%7B9AA7C048-D3D8-4C75-934A-D1C491432CE5%7D&Source=https%3A%2F%2F{siteurl}2%2FLists%2F{listurl}%2FAllItems.aspx&RootFolder=%2F{siteUrl}2%2F{listurl}&ID=1&ContentTypeId=0x0100B0E7B25049CA144199BDE02D715D0EC9001017085E671B4E46A1ABEA6141816A6D
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.
Output
The output of the function is https://contoso.sharepoint.com/teams/D-TEAM-Site
LibraryId / ListId
Add a ‘Initialize variable` action for libraryId/ListId and use the following expression to retrieve the libraryId/listId.
replace(replace(split(split(decodeUriComponent(triggerBody()?['entity']?['itemUrl']), 'ListId=')[1], '&')[0],'}',''),'{','')
Output
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/get item or perform other operations.
SharePoint file properties
SharePoint List Item properties
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
or ``For a selected item` 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.