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].
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:
if(variables('documentLocked'),variables('delaySeconds'),1)
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.
soumya says
hi , Do you know if we can run powershell scripts in powerapps
Vijay says
Hi Paul,
In my case user open document for 2-3 days then can we wait for 2-3 using flow.
Can flow run for 2-3 days is it create any issues ?
Regards
Vijay
Ade says
Can’t you do the same thing with the Retry Policy option?
Jay Burke says
Boss this fella
David Regis says
How do you notify the recipient that triggered the flow that the file is locked and by whom so they can contact the user to close it?
Claudia says
Hi Paul,
Nice explained 🙂
For David Regis:
you can find a possible solution if you open this link: https://www.thetindog.com/dynamics-as-we-go/identifying-sharepoint-file-locks-with-power-automate
Farooq says
Hello, Quick question please. I used the IF statement to check if the document was locked but I keep getting an error message that says “Correct to include a valid reference to ‘Update_file_properties’ for the input parameter(s) of action”. Can you advise on any fix? Thanks.
Scott says
I tried this approach with a locked file in a document library. The problem I had is that we use versioning, and every time the DO UNTIL loop reran, the document’s minor version incremented until it hit its max of 512 (We were waiting on an approval, and the person must’ve left the document open and forgot about it). Is there a way to implement the loop without incrementing the minor version?