When working with SharePoint, Power Automate brings a great set of actions to improve day-to-day processes. You can automate work by creating flows that:
- Start approval processes or request signatures
- Perform analysis using AI builder technologies
- Trigger processes depending on metadata changes
Sadly, this approach no longer works…
Gladly, there’s a new approach to deal with this scenario
Reuse Flows connected to SharePoint Lists – Working again
Building these types of solutions can save a lot of time for people in organizations, but what about saving time on development?
Imagine this scenario: you work in a Marketing department with 10 document libraries having the same requirement; every file needs a flow that changes the status and moves it to the Completed folder.
At first glance, this might seem like a simple task. However, you would need to create the same Flow for each library. What if later on, there is a change in the requirements? Or a new status appears, or we need to add another step to our Flow? Maintaining these Flows can be a challenging task.
Let’s see how we can reuse the same Flow for different document libraries. Keep in mind that this approach works the same on Lists or Document libraries.
Build your Flow
The first step is to create an Instant Flow. I’m going to use the sample requirements to build it.
- Go to Power Automate, create an Instant Flow, and Select For a selected File as the trigger. Choose the Marketing site and one of the document libraries.
- Add an Update file properties action to set the status as Completed.
- Move the file to a Completed folder inside the library.
Your Flow should end up like this:
Reuse your Flow
Right now, the Flow is connected to the document library that has been set in the trigger, and it is available through SharePoint’s Automate toolbar:
On Flow execution, the trigger action gets information about the selected file that started the Flow: ID, URL, Filename, and the File ID. However, we also need information about the site and the document library.
"body": {
"entity": {
"ID": 12,
"itemUrl": "https://powernimbus.sharepoint.com/Brochures/Forms/AllItems.aspx#id=%2FBrochures%2FBrochure%20%281%29%2Epdf&parent=%2FBrochures",
"fileName": "Brochure (1).pdf",
"FileId": "12"
}
}
We could dissect the itemUrl, but SharePoint sometimes gives you another URL type, for instance:
https://powernimbus.sharepoint.com/_layouts/15/listform.aspx?PageType=4&ListId=%7BF7B36715-D9B6-40EE-AAEB-89EFD927A0A3%7D&Source=https%3A%2F%2Fampicurrents.sharepoint.com%2FLists%2FMarketing%2520Emails%2FAllItems.aspx&RootFolder=%2FLists%2FMarketing%20Emails&ID=1&ContentTypeId=0x0100C58F40594BFF3C41AB8CB70CAFF4ADE4
Digging a little more, in the trigger data’s headers section, we could see a property called Referer that gives us two essential parameters: the SharePoint site and the List ID.
{
"headers": {
"Connection": "...",
"Accept": "...",
"Accept-Encoding": "...",
"Accept-Language": "...",
"Expect": "...",
"Host": "...",
"Referer": "https://emea.flow.microsoft.com/en-US/widgets/manage/environments/Default-fdc90c70-0811-412c-8551-e5706c9b92da/flow/run?widgetId=160c0087-bdbb-4080-b04f-68df-a7f6470c&sdkVersion=2.0&widgetType=flowsRuntime&utm_medium=widget&utm_source=SPO&environment=Default-fdc90c70-0811-412c-8551-e5706c9b92da&enableOnBehalfOfTokens=true&enableRegionalPortal=true&enableWidgetV2=true&oAuthHandler=oAuth&backgroundColor=white¶meters.sharepoint.site=https:%2F%2Fpowernimbus.sharepoint.com¶meters.sharepoint.list=bcd445a7-66ed-45a4-9d74-d61e47fbcff8&useServerSideProvisioning=true&pathAlias=flowsRuntime&allowOptionalEvents=true",
"User-Agent": "...",
"x-ms-client-session-id": "...",
"x-ms-client-request-id": "...",
"origin": "...",
"sec-fetch-site": "...",
"sec-fetch-mode": "...",
"sec-fetch-dest": "...",
"X-MS-APIM-Referrer": "...",
"x-ms-gateway-object-id": "...",
"X-MS-APIM-Tokens": "...",
"X-MS-APIM-Referrer-Prefix": "...",
"X-MS-APIM-Callback": "...",
"x-ms-user-id": "...",
"x-ms-user-name": "...",
"x-ms-user-name-encoded": "...=",
"x-ms-user-email": "...",
"x-ms-user-email-encoded": "...",
"x-ms-user-timestamp": "...",
"X-Forwarded-For": "...",
"Content-Length": "...",
"Content-Type": "..."
},
"body": {
"entity": {
"ID": 12,
"itemUrl": "https://powernimbus.sharepoint.com/Brochures/Forms/AllItems.aspx#id=%2FBrochures%2FBrochure%20%281%29%2Epdf&parent=%2FBrochures",
"fileName": "Brochure (1).pdf",
"FileId": "12"
}
}
}
https://emea.flow.microsoft.com/en-US/widgets/manage/environments/Default-fdc90c70-0811-412c-8551-e5706c9b92da/flow/run?widgetId=160c0087-bdbb-4080-b04f-68df-a7f6470c&sdkVersion=2.0&widgetType=flowsRuntime&utm_medium=widget&utm_source=SPO&environment=Default-fdc90c70-0811-412c-8551-e5706c9b92da&enableOnBehalfOfTokens=true&enableRegionalPortal=true&enableWidgetV2=true&oAuthHandler=oAuth&backgroundColor=white¶meters.sharepoint.site=https:%2F%2Fpowernimbus.sharepoint.com¶meters.sharepoint.list=bcd445a7-66ed-45a4-9d74-d61e47fbcff8&useServerSideProvisioning=true&pathAlias=flowsRuntime&allowOptionalEvents=true
Using these parameters, we can detect from which site and from which list we are being called. To get this data, we are going to use the trigger() function as it returns all the information regarding the Flow’s caller.
To extract this information, we are going to use three compose actions to get:
Referer URL
trigger()?['outputs']?['headers']?['Referer']
SharePoint Site URL
decodeUriComponent(first(split(last(split(outputs('Referer'), 'parameters.sharepoint.site=')),'&')))
SharePoint List ID
first(split(last(split(outputs('Referer'), 'parameters.sharepoint.list=')),'&'))
We can then use these outputs to get information from the list items or document library files. For instance, by using Custom values in Get file properties actions, in addition to the ID received in the trigger.
Putting in all together
Now that the Flow is library agnostic, we need a way to call it from the other document libraries. Using the toolbar’s Automate option is only available for library configured in the trigger of the Flow.
This is where SharePoint Column formatting comes to the rescue. It allows you to transform the render of any given list view column. Therefore, giving you the ability to display the data exactly the way you want it.
One of the features included is the ability to call Power Automate Flows using a custom row action called executeFlow. You only need to provide the Flow ID, which you can get by opening your Flow and looking at this portion of the URL:
https://emea.flow.microsoft.com/manage/environments/<environment-id>/flows/<flow-id>/details
To use it, add a calculated column in your document library with a dummy formula like this: =””. After that, format this column in your view to call the Flow. For example:
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "button",
"customRowAction": {
"action": "executeFlow",
"actionParams": "{\"id\": \"026a443e-d394-4457-b291-5134a981f90c\"}"
},
"attributes": {
"class": "ms-fontColor-themePrimary ms-fontColor-themeDarker--hover"
},
"style": {
"border": "none",
"background-color": "transparent",
"cursor": "pointer"
},
"children": [
{
"elmType": "span",
"attributes": {
"iconName": "Completed"
},
"style": {
"padding-right": "6px"
}
},
{
"elmType": "span",
"txtContent": "Mark as complete"
}
]
}
Your Document library will be able to call the Flow through a button:
Add this column to the rest of the document libraries and then, they all will be calling the same Flow. Sending the ID of the file that requested it along the way.
You can get the Flow that dissects the SharePoint parameters from my GitHub repository.
I hope this approach helps you with your Flow maintenance tasks ✌
Hi Eickhel,
Thank you so much for this solution!
I have one problem at the end of the process, when i upload a file.
When I insert a new file I have some more column with information, if I try to save after the update , SP return me that i can’t save it due to a validation error…. it’s something familiar for you?
Thank you
Riki
Hi Riki,
It depends on the validation error youbare getting.
What’s the error?
Hi Eickhel,
I solved it by changing the formula to: =1<2
Probabbly is related to the double qoute that we don't use here for string.
Thank you for your answer.
Hello Eickhel, thank you for this!
I’m not sure but my Referer URL in the header is truncated after https://emea.flow.microsoft.com/
is somwthing familiar to you? do you have a solution to this?
Thanks Mark
Hey Mark,
Sorry for the late reply.
I’ve noticed that the platform cleared the referer URL. I do have another workaround, I’ll try to add another post next week.
Hi Eickhel, nice post!
Would you be able to share the workaround for the cleared referrer Url?
Yes, please check the new post 🙂
https://www.powernimbus.com/2021/03/reuse-flows-connected-to-sharepoint-lists-working-again/