Trigger a Flow from Multiple SharePoint Libraries

In this post, I’ll show you a way that a flow can be set up to manually trigger the same from from multiple locations in SharePoint.  Here’s the back story.  I created an approval workflow for a committee’s SharePoint site, for approving meeting minutes.  Then, another committee wanted the same flow, then another and another.  I ended up creating and managing about 6 different flows that did the exact same thing, on multiple SharePoint sites.  This became painful anytime updates / tweaks needed to be made to the logic or verbiage in the flows.  I had to make all the changes six times.  When a seventh committee wanted this flow as well, that’s when I decided to try and build a more efficient way.  Somebody named Mark Boes, who had read my blog, had sent me a random email back in October, saying that after reading my post about how to create a button in a SharePoint list to trigger a flow, he came upon the realization that the ‘For selected item’ action receives the itemURL from where the button was pressed, regardless of the action Site Address.  Thanks Mark!  I thought this scenario would be a good opportunity to try this out.  Here are the steps:

1.  Create a flow, and use the SharePoint trigger For a selected item.

2.  Pick any site and list or library here.  It doesn’t matter what you pick.

sharepoint-for-selected-item-trigger

3.  We’ll be initializing seven different string variables.  The first one is for the site root, which is the part of the url that comes before the name of your site.  This isn’t dynamic, you’ll be typing your own in here, as the default value.

flow-initialize-variable-site-root

4.  Initialize a variable for the item URL.  This is buried in the trigger, here’s the syntax to get it.  Note that I’ve typed the formula there in the comments of the action as well, for easy reference.
triggerBody()?[‘entity’]?[‘ItemURL’]

flow-variable-item-url

Note:  The ItemURL itself looks something like this:

https:// wonderlaura.sharepoint.com/teams/CommitteeC/_layouts/15/Doc.aspx?sourcedoc=%7Babb5b0ad-4a30-45a2-9e4e-f253f784abec%7D&action=edit&uid=%7BABB5B0AD-4A30-45A2-9E4E-F253F784ABEC%7D&ListItemId=1&ListId=%7BC4D850F3-4E4A-4776-9980-1709B1A794EB%7D&odsp=1&env=prod
You may wonder why, in my formulas, I don’t just look for the _layouts in the URL.  Well, after I first implemented this solution, I noticed that depending on the view that I was in, or how I got to the item, the URL was structured completely differently and did not contain the _layouts.  So unfortunately, it’s not something that you can reliably depend on.  Same goes for the ListId in the URL.  It’s not always there.

5.  The next four variables just need to be initialized, but with no default value.

flow-more-variables
flow-variable-site-title

6.  Initialize one more variable, for the name of the library that the file is in.  In my solution with the committees, it’s easy because it’s just the “Documents” library for all of them.  In the associated video with this blog post, I explain why there isn’t a way to divulge the name of the current list/library just from the ItemURL.

flow-variable-document-library-name

7.  Add a Compose action, and put together the full URL to the current site, using this expression.  Note that I also put the expression in the comments section of my action, for easy reference.
substring(variables(‘varItemURL’),length(variables(‘varSiteRoot’)),indexOf(substring(variables(‘varItemURL’),length(variables(‘varSiteRoot’))),’/’))

flow-compose-site-name

8. Set a variable.  This is where we concatenate the varSiteRoot, from step 3, along with the URL to that specific site.  As I mention in the video, there are probably various ways that these formulas could have been configured to achieve what we need, this just happens to be the way I did it.
concat(variables(‘varSiteRoot’),outputs(‘site_name_url’))

flow-variable-site-url

9.   Now, we have the url of the site and the name of the library called Documents, which we keep in a variable varDocLibraryName just because it makes it simpler since we’re using it multiple times.  Now it’s time to get the file’s properties.  SharePoint action Get file properties.

flow-sharepoint-get-file-properties

10.  We have to get the filename from the file properties in step 9.  Here’s the formula.  If you’re watching the associated video (below), you’ll notice that I showed a much longer way of achieving this, but this formula is really all you need, it’s the simplest way.
body(‘Get_file_properties’)?[‘{Name}’]

flow-set-variable-filename

11. Next, the approval action can be added.  Who do you want the approval to go to?

image

When it comes to whom to assign the approval to, this widely varies.

If you would like the person triggering the flow to manually pick an approver right when they trigger the flow, at step 2 above, add an email input.  Use that value in the assigned to box above.

If you would like the approver to be the person who triggered the flow’s manager, then you can use the Get Manager action instead.

If you’d like to use a person from a person column in the library, you’ll have that data from the get file properties action at step 9.  There are many, many potential ways of getting / setting who it needs to be assigned to.

What do you want to happen after the approval?  Of course that also varies.  Send an email, and set the content approval status are two common ones.

Here’s a screenshot of the whole thing

generic-flow-full

Now, how do we trigger the flow?

In my example, I created a column called Approval.

Check out my post how to create a button in a SharePoint list to trigger a flow for the instructions to create a button in the library to kick off the flow.  In my real-world example, like I mentioned, each committee uses the Documents library on their site, where they keep the meeting minutes, so I just added this button to that Approval column in each library on each committee site.  They all kick off this exact same flow, so no need to create a separate flow for each library.

Here is the full video where I demonstrated this solution on SharePoint Power Hour:

power-hour-generic-flow

Note: Since I did this demonstrated this yesterday, I’ve actually fine tuned it a bit more as I wrote down the instructions, for example, I added the variable root site instead of manually typing the number of characters and all that.  And the second half where I get the site and group and all that, I’m going to write as a separate blog post.

14 comments

  • Thanks for doing this detailed blog. I know I’ll be setting up this type of Flow for my company.

    Merry Everything and Happy Always!

    Lisa

    Like

  • thanks for this. Although I don’t need to do this is t was a good intro to using variables which I have not done before. Even with your notes, writing the string expressions to populate these variables has proven hard. Is there a good tool for checking and debugging these?

    Like

  • Thank you for sharing. This is really helpful. I would like to use it in combination with the step “update file properties”. Unfortunately I get an error: “Cannot evaluate the parameter ‘path.$.dataset’ at design-time; it is empty or has a runtime expression.” Do you have any ideas on that?

    Thomas

    Like

  • Thank you for at great video and blog post.

    Im nearly there… and i think ive finally found the missing piece. My document library is in a subsite… so i guess i missing something in step 4 that specifies the subsite? or am i completely off?

    the URL looks like this:
    …..sharepoint.com/sites/Portal_Prototype/Byggesag1/Documents/

    /byggesag1/ doesnt seems to be included in sted 4.

    I hope you guide me in the right direction.

    Like

    • In mine, I have no sub-sites. Since Microsoft has deemed that sub-sites are no longer to be used, it’s not something I thought to accommodate for in my logic. I’m not sure how you’d make it dynamic if there could randomly be sub-sites involved.

      Like

  • Pingback: Power Automate Approval to Office 365 Group Members | @WonderLaura

  • I have been using SharePoint 2016 and I am looking at moving to SharePoint Online. One of the features that is not available in SharePoint Online is the ability to create site templates. Is there a workaround for this for SharePoint Online? We are currently creating a new site for each project and using templates (sites, lists and libraries) to create a site for a new project.

    Like

    • It’s possible to create site design templates that come with predefined libraries and lists

      Like

  • Hi Laura,

    Thank you for taking time with this, it’s the simple realisation that the “For a selected item” trigger is generic, that brings the power here. I will be making extensive use of this.

    I wanted to loop back and share a different approach to getting the parameters so you can easily pick the ones you want. This formula takes the Querystring Parameters from the Item URL and turns it into a JSON string. You can then add a ParseJSON activity to use the named parameters:

    concat(‘{“‘, replace(replace(split(triggerBody()?[‘entity’]?[‘ItemURL’],’.aspx?’)[1],’=’,'”:”‘),’&’,'”,”‘),'”}’)

    For those who are new to formulas or struggle with nested functions….We basically just replace all of the querystring separators with JSON equivalents:

    & becomes “,” – used to separate one item in the JSON from the previous
    = becomes “:” – used to separate the item value from the item name

    We then top and tail the whole string with {” and “} to signify the JSON object and start and close out the item name for the first and last item. This gives us the JSON string:

    {“sourcedoc”:”%7Bf0dxxxxc-9658-xxxx-xxxx-e8831afc18f8%7D”,”action”:”default”,”uid”:”%7BF0DxxxxC-9658-4F13-ABF5-ExxxxAFC18F8%7D”,”ListItemId”:”1″,”ListId”:”%7B7D68E373-xxxx-4B04-xxxx-45C554442E76%7D”,”odsp”:”1″,”env”:”prod”}

    Which we can place as the schema string for a Parse JSON activity, producing a nice object:

    {
    “sourcedoc”: “%7Bf0dxxxxc-9658-xxxx-xxxx-e8831afc18f8%7D”,
    “action”: “default”,
    “uid”: “%7BF0DxxxxC-9658-4F13-ABF5-ExxxxAFC18F8%7D”,
    “ListItemId”: “1”,
    “ListId”: “%7B7D68E373-xxxx-4B04-xxxx-45C554442E76%7D”,
    “odsp”: “1”,
    “env”: “prod”
    }

    We can now reference each item in the JSON object by name when we need to use them:

    body(‘Parse_Parameters’)?[‘ListId’]

    We just need to remove the GUID escape characters %7B and %7D if we use those elements.

    The same sort of approach gets us the Site path too:

    split(triggerBody()?[‘entity’]?[‘ItemURL’],’/_layouts’)[0]

    Of course we can also extend that to get the root if that’s required as well; just using ‘/’ as the split character.

    Hope this helps someone else out.

    Stay safe everyone.

    Like

  • Lourenço de Azevedo Meireles Pinto da Silva

    I can´t get the items URL, When I use the following expression in step 3: triggerBody()?[‘entity’]?[‘ItemURL’]
    I get an error message saying :The expression is invalid.

    Like

    • Hi Lourenço, try deleting the quotes ‘ and then retyping them in, they look like they were reformatted in the post comments so are probably invalid characters in flow even though they look sort of correct.

      Like

  • Thanks for the info its been a great help, The site Url wasn’t working for me however I’ve found if part 4 triggerBody()?[‘entity’]?[‘ItemURL’] to sharepoint link to item this solves that issue.

    Like

Leave a Reply to Lourenço de Azevedo Meireles Pinto da Silva Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.