If you are updating the status of a document in a Microsoft Power Automate flow that someone is already using, you will get this error:
The file "filename.docx" is locked for shared use by [email protected]domain.com.
and it will return a HTTP Status code of 400:
Below is a method that waits for the file to be free and then moves to the next step:
Wait for the file to be unlocked
Here is an example flow which updates the status of a file stored in SharePoint. If the file is available it will work normally. If it is locked it will loop for an increasing amount of time before attempting the operating again:
Here is how it works:
- A Boolean variable is created called “documentLocked” and its value is set to True. This variable is used to control the loop.
- An integer variable called delaySeconds is created and its value is set to 1. This variable defines how long the loop will wait for before attempting to access the file again.
- A do until loop is then started which will continue until the value of documentLocked is equal to false.
- The “Update File Properties” action is executed as normal.
- The next action is configured to run if “Update File Properties” was successful, or if it failed:
An expression is used to determine the value of the variable based on the locked status of the document.
- An increment variable action is then used to increase the delay time on the subsequent loop by 60 seconds. Each iteration of the loop will create a longer delay.
The expression used in the “Set Variable” step is:
if ( and ( equals(outputs('Update_file_properties')?['body']?['status'],400), contains(outputs('Update_file_properties')?['body']?['message'], 'locked') ) ,true ,false )
The condition checks to see if the output of the “Update File Properties” resulted in an error code 400 and the error message contained the word “locked”. If both of those conditions are true then documentLocked is set to true, otherwise it is set to false.
Both the status code and message are checked because HTTP Status code 400 is vague and the error might not actually be related to a file lock.
The expression in the delay action is:
This condition checks if the document is locked:
- If it is locked, delay seconds is set to the value of the variable delaySeconds.
- If it is free, then it is set to 1.
I’ve used this technique a number of times and it’s worked really well, and is simple to implement.