Identifying Sharepoint File Locks With Power Automate

The amazing Power Platform can connect to almost any aspect of Microsoft 365 and use their API. Here’s an interesting proof of concept I had to implement for a client that had a rather complex Sharepoint process triggered by a Dynamics365 record.


Problem: When running Power Automate file rename functions against Sharepoints API, how do you identify a file lock prior to attempting to rename the file?

One Solution: I used the Send HTTP Request To Sharepoint block to query the Sharepoint API and retrieve the LockedByUser information. This is returned NULL when it is not locked. If there is a file lock, the LockedByUser node will not be returned at all within the JSON response, however you will get a UserPrincipleID, so you will know who is locking the file.


Here’s the Power Automate proof of concept.

Add your trigger - in my case I used the manual trigger to make this.

Setup your variables to hold some info. The first is the path to the actual file. In my case it was

/sites/TheTinDog/Shared Documents/TestFiles

The second variable is to hold the file name. In my case it was

TestDoc1.docx
Screen_Shot_2020-11-30_at_9_17_29_PM.jpg
Screen Shot 2020-11-30 at 9.17.44 PM.png

Next we add a couple of variables to hold the responses we’re going to get.

Once to hold a boolean if the file is locked, the second to hold the username of the person locking the file.

Next up, is the meat of the flow. This is an HTTP request to the Sharepoint API, to specifically get the locking information

NOTE: This will only be effective if the file has been opened by the user. If the file has been checked out, you need to use a different method.

Add the Send an HTTP Request to Sharepoint step and connect it to your site address.

Set the Method as GET.

In the URI enter: (Replace the italics with the actual variables you have used)

_api/web/GetFolderByServerRelativeURL('⁠SharepointPath⁠')/files/getbyurl('⁠SharepointPath⁠/SharepointFilename⁠')/LockedByUser

Screen_Shot_2020-11-30_at_9_17_55_PM.jpg
Screen Shot 2020-11-30 at 9.18.11 PM.png
Screen Shot 2020-11-30 at 9.18.17 PM.png

Next, we need to work with the responses we’re given. Ive added a compose step to convert the response to a string.

string(outputs('Send_an_HTTP_request_to_SharePoint')?['body'])

Then a second compose step to search for the text ‘LockedByuser’ - this is only returned if the file is NOT locked.

contains(outputs('Convert_HTTP_Response_to_String'),'LockedByUser')

Finally, we can check a condition to carry out our relevant actions with the responses. If it is not locked, then we can go ahead with our next processes. If it is locked, maybe we need to tell someone.

We can access the username of the offending user by grabbing it from the JSON response.

outputs('Send_an_HTTP_request_to_SharePoint')?['body']['d']['UserPrincipalName']

Screen Shot 2020-11-30 at 9.18.50 PM.png
Previous
Previous

Dealing With Failed Steps Gracefully

Next
Next

By any-other, other, other name