Using dynamic SharePoint sites and lists in your Flows

If you have some experience with Power Automate the concept of Dynamic Content will be familiar to you. All Flows make use of this concept, where you use output of data from a previous action as input for another one.

When using the SharePoint connector this is often used for determining the details of items and documents that are created or updated in the Flow.
But did you know you can also dynamically determine the site and list or library you want to perform the action on?

But why?

Well ‘Why should I do that?' you might wonder. Just some examples:

  • You are doing a lot of different actions on the same site. Defining the URL once and reusing that as dynamic content makes sure you do not have to reselect the same site for every action. 
  • You want to deploy your Flow from a Development, to a Test,  Acceptance and Production environment where the site URL differs per environment and you do not want to update the Flow in every environment. 
  • You want to do a similar action on different sites and lists or libraries in the same Flow and want to make use of a for each loop instead of having separate actions for every site and list.

In all these cases making use of dynamic content will make your life easier and your Flow cleaner and easier to reuse. If you are used to working with the SharePoint REST API, using the Send an HTTP Request to SharePoint action could be used to do whatever you like to do. But in this blog I want to have a look at how to use the ‘normal' SharePoint actions with dynamic input.

How to do it

Step 1 Collect the site URL

Add an action where your site URL is added or collected. For trying out this technique you can start by using the Trigger Manually trigger a flow and add a text input to add a site URL when you trigger the Flow.

Step 2 Load lists and libraries

When working with a static site URL the SharePoint actions can load the lists and libraries on your site. When using a dynamic site URL this is not the case. So the next step is to get all site lists and/or libraries of the site and filter down the target list(s).

Instead of searching for or picking a site from the list of site addresses use the option + Add a custom item and add the trigger field containing the site URL as dynamic content.

The screenshots are created using Power Automate with experimental features switched on. If the UI looks different in your case, look at the bottom of the list of site addresses for the Enter custom value option.

Step 3 Filter lists and libraries

Next step is to filter down the list(s) to only get the target list(s). The Get Lists action does not have a filter option. Instead you can use the Filter array action for this purpose.

Step 4 Add the action

All prepping is done. Now we can add the action we want to perform on the filtered list. For example the Create item action. Use the the Site URL output of the trigger action again as dynamic value for the Site Address.

Now when I click on the List Name field to open the fly out an error message tells me there is a problem with loading the lists on the site, but you can still use the + Add a custom item option. You can select the Name output of the Filter array action directly, but when you know your filter will always give one result as output I prefer using expressions to prevent a For each loop to be added automatically.

Expression syntax

first – expression for only getting the first item of the array

body – expression for referencing the body of a previous action

‘Filter_array – The name of the action where you want to use the output from, between single quotes and the spaces replaced by underscores

[‘Name'] – the field value that we want to use from the first item of the array, between square brackets and single quotes. This field needs to be after the closing parentheses of the expressions.

Step 5 Construct the item object

When using a static site URL and list the Create item action would at this point based on the schema of the list show fields for all the columns. When using dynamics inputs the action cannot do this, since this information will only be available when actually running the Flow. Now there is only one field called Item. All the fields of the item can be added to this field in one go using the following syntax:

{
   'field name': 'value',
   'field name': 'value',
   'choice field name': {'Value': 'Choice value'},
   'field name': 'value',
   etc.
}

You can obviously type this all out if you know the correct names and data types of the fields in your list. I use a short cut to get these details by temporarily adding a Create item action using a static site and list value and adding some random values to the fields I want to use. The Peek code option of this action will show most of the syntax that I need to add to the Item field.

A couple of things need to be updated compared to what the Peek code view shows. The item/ text in front of every field needs to be removed and choice columns need to be changed from “ChoiceColumn/Value”: “Something” to “Choice column”: {“Value”: “Something”}.

For the field values you can use dynamic content, expressions or just plain text.

Don't forget to remove the temporary action from your Flow.

Try it out

The Flow is done. Now lets test run the Flow and see if it works.

It looks like it. 😊

Summary

In this blog I shared how to work with dynamic inputs for the ‘normal' SharePoint actions in Power Automate.

Obviously this is a rather cumbersome set-up if you have just a couple of actions to perform and you know up front changes are low the site or list will change. In that case, just use static values for your the site addresses and list an library names.

But if you are in the process of creating multiple versions of the same Flow or adding a lot of steps that perform actions on the same site consider using this technique. It might make your life easier in the long run.

Download

Download and example Flow that uses the technique described in this post.