The “For a selected file” trigger in SharePoint Power Automate lets you start automated Power Automate flow right from a file you pick in a SharePoint library. These flows can do things like send alerts, update info, move files, or approve documents.
But to customize better, you need more info, like the Site URL and List details.
When this trigger kicks in, it gives you two main things: the file’s web link and its name. From the web link, you can figure out the SharePoint Site URL and the Document Library name.
The SharePoint Site URL and Document Library name are important because most actions in Power Automate need them to work. They help Power Automate find the right place in SharePoint to do tasks like moving files or updating information. So, knowing these details ensures your automated workflows run smoothly without any hiccups.
In our flow, we start by extracting the SharePoint URL and SharePoint Document Library Name from the ItemURL provided by our Trigger.
Here are the expressions:
Document Library URL:
join(take(split(triggerBody()?[‘entity’]?[‘itemUrl’],’/’), 5),’/’)
Document Library Name:
replace(split(triggerBody()?[‘entity’]?[‘itemUrl’],’/’)[5],’%20′,’ ‘)
What I’ve noticed is that every SharePoint Item URL contains both the library name and the SharePoint Site. However, these URLs typically come in two types, depending on the file type they represent:
- URLs without “_layouts” are typically for file types like PDFs, images, and so on.
- URLs with “_layouts” are usually for file types such as Word documents, Excel spreadsheets, Visio diagrams, PowerPoint presentations, OneNote files, and more.
For further clarification, please refer to the accompanying images.
The issue with using the Document Library name is that if it changes, the name in the ItemURL remains the same as when the Document Library was created. This can lead to flow failures.
Even if the Document Library name changes, there’s a unique ID linked to it, which we can use in SharePoint actions instead of relying on the Library’s name.
To obtain the Document Library’s ID, we require the FileID, which is distinct from the one triggered by the flow. For URLs lacking “_layouts,” we retrieve file metadata using the path, extracting the FileID from it.
Expression: Get file guid from metadata :
replace(replace(replace(split(outputs(‘Get_file_metadata_using_path’)?[‘body/ETag’],’,’)[0],'”‘,”),'{‘,”),’}’,”)
For URLs containing “_layouts,” the FileID is readily available within the URL; all we need to do is decode the URL and extract the FileID.
Expressions:
Decode library URL:
decodeUriComponent(triggerBody()?[‘entity’]?[‘itemUrl’])
Process decoded library URL:
slice(outputs(‘Decode_library_url’), add(indexOf(outputs(‘Decode_library_url’), ‘?’), 1), indexOf(outputs(‘Decode_library_url’), ‘&’))
Get FileID:
slice(outputs(‘Process_decoded_library_url’), add(indexOf(outputs(‘Process_decoded_library_url’), ‘{‘), 1), indexOf(outputs(‘Process_decoded_library_url’), ‘}’))
As we now have the FileID we can get the Document Library ID by using the API
_api/web/GetFileById(‘@{variables(‘File ID’)}’)/?$expand=ListItemAllFields/ParentList
The Document Library ID is stored in the Process document library Id stage
Expressions
Process document library Id
outputs(‘Get_document_library_updated_name’)?[‘body’]?[‘ListItemAllFields’]?[‘ParentList’]?[‘Id’]
Now that we have the document library ID and the Site URL we can use it in various SharePoint Power Automate Actions, for example i am using it to set the content approval status to submitted.
Hope this was a helpful read 😊! Please leave your comments and suggestions in the comments below.