Flow error: File is locked for shared use
When you are working with files in a document library in SharePoint, if your Power Automate flow is trying to update the properties of a file, and the file is currently already open by someone who is looking at it or editing it, your flow will give you the error (400) that the file is locked for shared use by ___ and it will have the user’s email address.
When I noticed this happening in a flow that I built for a document approval process, I went ahead and figured out a way to make the flow detect if the file is locked, and if so send an email to that person, wait 10 minutes and try again.
Here’s the flow error and how it works
Basically, when the flow gets to the part where the file properties need to be updated, we need to determine first if the file is currently in use, and if the status error code is 400, which is the exact error that it gives.

In my example, I’ve got a flow with an approval action, and then the next step in the flow is to see what the outcome was, such as approved or rejected. My flow then has a condition that looks to see if it was approved, and if so it updates the status column of the document in SharePoint, to say the name of the next status, and if it’s rejected there is an update file properties action that sets the status column to say that it was rejected. SO, instead of doing all of this new logic inside of every condition and every update action, I’m going to immediately check, right after the file is approved or rejected, to see if the file is locked. I’m going to do this with the file check out action.
In my previous 3 part series, I built a three stage approval process for documents in a library in SharePoint. So, in all of my example screenshots, I’ll show exactly where I made adjustments to this already existing flow.
Here’s the original flow BEFORE all of this new logic I’m about to add:

Ok let’s build in this new functionality for document errors.
Step by Step
1. Initialize a string variable, varFileStatus. This goes right after the flow’s trigger.

2. Right after the flow’s approval action, add a Do Until loop.
Loop until the varFileStatus is equal to NoError
and notice here that I adjusted that default timeout, so that instead of one hour, it times out after 4 hours.

3. Inside of the Do Until, add the SharePoint action Check out file.

4. After check out file, then we need to see if the flow gets an error. If someone currently has the file open, it will error on the check out file action. Add a condition.

Note that here’s how I typed the status as an expression, and then after saving the flow, it rendered as a regular green dynamic field as you see above.

5. In order to make sure that the flow keeps flowing even when it hits an error, go to the condition’s Settings tab, and add a checkbox next to Has failed.

6. This next part is for troubleshooting and for your information as you’re creating and testing this flow. Add a Compose action in the TRUE part of the condition, and name it ComposeError. I typed the expression in that one, just like the status one above.

7. Test the flow. Start the flow, then before approving the file, open the file. When you approve it, then the flow will error and fail. This is what you’ll see in the compose action.

8. Now that you’ve mocked up the error, and you’ve seen it happen and you know exactly what the error message says, the next step is to extract that email address out of that string of text, so you’ll know WHO has the file open. Add another compose action (name it Compose Email) after the last one, and use this expression:
substring(
outputs('ComposeError'),
add(indexOf(outputs('ComposeError'),'shared'),14),
sub(IndexOf(outputs('ComposeError'),'[membership]'),
add(indexOf(outputs('ComposeError'),'shared'),14)
)
)

Basically, that code finds the email address by finding the word ‘shared’ in the error and the [membership] in the error, and getting the substring of the email address between them.
9. Next, add a send an email action. The “TO” is the output of the Compose Email action.

10. Add a delay. I want the flow to wait 10 minutes, see if the file is still checked out, and send another email if so. With the way I set the do until loop to time out after 4 hours, so this loop will keep looping and will send the person an email once every ten minutes for 4 hours. Feel free to change this to be less frequent.

11. Since we’re using the Check out file at step 3 as a tool to see if the file is currently open, we do need to be sure and check the file back in. Here are the settings for the SharePoint Check in file action, to take place after the approve/reject condition.

Here is the final screenshot of the whole thing. Notice that we didn’t do anything inside of the Condition approved section, that’s where it updates the SharePoint file’s status column to a status according to the outcome of the approval.

Here are my 3 previous blog posts regarding the 3 stage approval:
1. Document one click approval trigger
2. Trigger conditions for optimizing flow efficiency
3. Three-Stage Document Approval Process in SharePoint with Flows
Happy troubleshooting!
Check out my very own online course if you’d like to learn more: