Trigger One Flow From Multiple SharePoint Lists

This post was inspired by a situation I faced at one of my customers. I was migrating SharePoint 2010 to SharePoint Online and in one site customer had many lists, where users were able to create items. Every time someone created or updated an item, SharePoint Designer workflow was sending them confirmation e-mail. Case was, that the lists were using the same workflow simply replicated per list.

Of course, I could simply create multiple flows doing the same job, but then I thought, this is just a matter of time customer would need to change something in the logic of flow, so I would then need to update {n} workflows. Plus, I love optimizing whatever is possible. So my idea was to simply find a way to create one flow, that could be triggered from multiple SharePoint lists.

After a short research I found this Twitter thread from John Liu:

And just moment later, his awesome post describing how this goal can be accomplished by using SharePoint lists webhooks and subscribing Power Automate flow to them: http://johnliu.net/blog/2019/3/one-flow-to-handle-them-all-how-to-subscribe-to-multiple-sharepoint-lists-with-one-flow.

The general idea is to create two flows:

  1. Subscriber: Triggered on a schedule, that checks if Handler flow is subscribed to specific list webhook and if not – subscribes it.
  2. Handler: Triggered via HTTP request, that is subscribed to list’s webhook and that is triggered whenever a change happens on that list.

HOW IT WORKS

Important! The whole solution requires Power Automate premium license, because Handler flow is triggered using premium action. The more users will use the solution, the more consider using “per flow” subscription instead of “per user”, as the whole solution might be considered as company-wide.

Basically what John described works like a charm, although I was missing very much a piece, that would help me to precisely get the items which were actually created/ modified when the Handler flow is triggered, but after it was triggered before. The point is, webhook only sends information, that event occurred on the specific list, but doesn’t send eg. list of items’ IDs, that were affected. For that John suggested, to simply get all items modified in last 10 seconds. This was not the best idea, since sometimes Handler flow was triggered after eg. 2 minutes. Then John provided link to Leo Siddle‘s post, where he describes in details how to use 

Folder.GetListItemChanges endpoint and Change tokens: https://gist.github.com/zplume/1baf04cc05927b57a5da248454b15dcc

To me it was a bit too complex and I was looking for something a bit easier.

HOW I DID IT?

How to actually trigger one flow from multiple SharePoint lists? It’s easier as you may think!

Subscriber flow

First the Subscriber flow. What I changed was that I also defined which lists should be monitored and should have the Handler subscribed to their webhooks. To do that, I defined a piece of odata query, to get only lists with specific guids:

and (Id eq guid'ec080920-5d86-4739-a14f-34298bcebfc7' or Id eq guid'a6ffb3c0-6b5c-4eb8-b4db-6b477f619485' or Id eq guid'f2d3252c-d00f-4da8-aabd-f2dd96029780' or ....)

Then I used “Send an HTTP Request to SharePoint” action, to get details of the lists I want to monitor:

_api/web/lists?$filter=BaseType eq 0 and Hidden eq false and (Id eq …. )

Note! BaseType eq 0 means lists.

Then I used filter action to get lists IDs (John used Titles, I think IDs are more secure) and finally for each ID I generated URL to call list’s Subscriptions endpoint:

Next, for each Subscription link, first I am getting its details like John did, then getting its ‘clientState’ value and then I am also getting the ‘resourceId’ – so the current list guid, to later use it as a value for “resource” parameter when registering new webhook:

  1. This is the current list’s GUID extracted by the ‘Compose’ action above.
  2. This is URL of the Handler flow, copied from its trigger action.
  3. This is information, how long should be the subscription valid. I am setting it for 30 days. After that, it will be unsubscribed.
  4. This is guid of the Subscriber flow, that is used to check, if for given list subscription done by the flow is present. You can get it by using the workflow()?['name'] expression.

Handler flow

Flow is triggered using the “When a HTTP request is received” action. Next steps are the same as John described in his second post: http://johnliu.net/blog/2019/5/one-flow-to-handle-them-all-part-2-figuring-out-the-changes and they are really good explained.

Now this is where I put in my two cents. Instead of getting all items from specific list modified in last 10 seconds OR following complex solution described by Leo, I decided to use just a SharePoint list, since the whole solution is for SharePoint anyway. My list is built from the following columns:

  1. Title – name of the list being monitored.
  2. GUID – guid of the list being monitored.
  3. LastModificationDateTime – timestamp when last time something on the list was changed. For the first run I set it to current date and time.

Next in my Handler flow, I am querying that list to get LastModificationDateTime for list which triggered the flow (using GUID as filter) and then I am updating the last modification date to the current datetime:

What I am next doing is getting a list of all items that were modified between the previous “LastModificationDateTime” moment and current datetime. And lastly, for each such item I am executing those steps, there were common for all workflows I found in all SharePoint 2010 site before it was migrated:

This works like a charm! I highly recommend you this way of handling situations, that you have multiple lists/ libraries even in different sites, that are using exactly the same workflows. Instead of renaming and copying existing flow per each list, simply follow the Subscriber – Handler approach. It’s much more elegant and way easier for maintenance and development.

Let me know what you think and if you find it useful in the comments below. Thanks!

About the Author:

Hi, I am Tomasz. I am expert in the field of process automation and business solutions’ building using Power Platform. I am Microsoft MVP and Nintex vTE.

Reference:

Poszytek, T. (2020). TRIGGER ONE FLOW FROM MULTIPLE SHAREPOINT LISTS. Available at: https://poszytek.eu/en/microsoft-en/office-365-en/powerautomate-en/trigger-one-flow-from-multiple-sharepoint-lists/ [Accessed: 22nd April 2021].

Share this on...

Rate this Post:

Share: