Create flows with Power Automate to update Title field in libraries of newly created SharePoint site automatically

Posted by

The Title field / property of files in SharePoint document libraries is important for the search index, so it’s a best practice to set the Title field manually or automatically by Power Automate flow for instance. Using Power Automate means you need to create flows that trigger on created and modified files in the relevant document libraries and that is a challenge to manage by hand. So I’ve been looking for a way to Power Automate this.

The first step is to create a template flow that will update the Title field with the file name when a file is created or modified. Here is the overview of this simple flow.

It’s important to add trigger condition(s) to the trigger, so the flow is triggered only when the condition(s) are met. For example when the Title and Name fields are not equal

@not(equals(triggerBody()?['Title'],triggerBody()?['{Name}']))

or when the Title field is empty

@empty(triggerBody()?['Title'])

Add the trigger condition(s) via the Settings option of the trigger.

The next step is to create the flow that can be called from your site provisioning process. In case you are using site designs and site scripts, or site provisioning engine and templates it’s possible to call Power Automate flow in / at the end of the process. Here is an overview of the flow.

Create the flow with a ‘When a HTTP request is received’ trigger and follow steps 1-5 from the tutorial Calling Power Automate from a site script.

Use the webUrl to set as custom value for the Site Address in the SharePoint action Get all lists and libraries.
Then use the Filter array action to select the document libraries (Type = 101) from the output of the previous action, with the following result:

In the next action we get the template flow ‘Update Title field’. We need to modify the path values for the trigger and action to let the Flow Definition refer to the webUrl of the new site and the guid of the libraries.

“path”: “/datasets/@{encodeURIComponent(encodeURIComponent(‘https://server.sharepoint.com/sites/web‘))}/tables/@{encodeURIComponent(encodeURIComponent(‘0c50cdd6-____-____-____-b1af844e8cda‘))}/items/@{encodeURIComponent(triggerBody()?[‘ID’])}/patchfileitem”

In the first Compose action we replace the url in the Flow Definition from the template flow with the webUrl of the newly created site.

replace(string(body('Get_Flow')['properties']['definition']),'https://server.sharepoint.com/sites/web',triggerBody()?['webUrl'])

Then for each library from the Filter array body we take the Name field to replace the guid in the output of the first Compose action.

replace(outputs('Compose:_Replace_webUrl'), '0c50cdd6-____-____-____-b1af844e8cda', items('Apply_to_each_Library')?['Name'])

The last action is to create a flow and set the Flow Display Name as follows:

concat(items('Apply_to_each_Library')?['DisplayName'],': ',body('Get_Flow')['properties']['displayName'])

and add the Flow Definition that is composed to refer to the library:

json(outputs('Compose:_Replace_library_guid'))

Use the connectionReferences from the Get flow action to set the connectionReferences. Make sure that the connection (of the template flow) is created for/with a service account that has sufficient licensing and permissions.

So the flow is ready to be tested and I use a simple flow to call the flow with a specified webUrl.

The called flow has run successfully and five flows were created.

Now when a file is created / added or modified the Title field will be updated with the file name when the trigger conditions are met.

The advantage of the Update file properties action is that the versioning in SharePoint isn’t triggered.

Considerations

Updating the Title field with Power Automate flow is a quick & dirty solution, since the asynchronous service doesn’t respect the events that might be happening on the file in the SharePoint library. So the update action will fail when the file is locked by a user. A dedicated SharePoint solution based on remote event receivers would be a better option, but is out of scope for me/this blog.
The SharePoint trigger is based on polling with an interval of 1 minute, so there’s a delay before the flow is started. SharePoint list webhooks are not a realistic alternative to use in combination with flow.

Addendum

It’s possible to call the SharePoint REST API to check whether a file is locked by a user. This can be used to extend the template flow ‘Update Title field’ and prevent that the flow fails when the file is locked by a user.
Add the following actions after the trigger.

Use the body from the output of the ‘Send an HTTP request to SharePoint’ action when the file is locked as sample to generate the JSON schema.

In the condition we check if the UserPrincipalName is empty to determine if the file is not locked by a user.
If yes, the Title field is updated with the file name. If no, then the flow is cancelled (instead of failed).

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *