Handling file locks in SharePoint

Have you ever struggled to handle file locks in SharePoint when you are running your flows?

File locks and actions in Power Automate

Power Automate’s actions that deal with documents in SharePoint, will have to deal with the same issues that users may deal with, however users have the SharePoint UI that will help them. In flows you will need to deal with it!

In SharePoint there are a number of file locks that may happen:

  • A user checks a file out
  • Another user checks the file out
  • A System Account checks a file out (this can happen when you declare a file as a record)
  • A user locks the document by opening a file

What happens when a file is locked?

First of all, what happens when a file is locked in SharePoint and a flow tries to update properties?

The request will fail:

Bad Request Locked

When a document is locked (or open by a user in a browser or client app) for editing  then you will receive the following:

The file \”https://pieterveenstramvp.sharepoint.com/Shared Documents/Document.docx\” is locked for shared use by pieter@pieterveenstramvp.onmicrosoft.com.
When I user has a file checked out then you will see this error instead:
The file \”https://pieterveenstramvp.sharepoint.com/Shared Documents/Document3.docx\” is checked out for editing by i:0#.f|membership|testuser@pieterveenstr.
When a document is declared as a record you will get the following message:
The file \”https://pieterveenstramvp.sharepoint.com/Shared Documents/Document.docx\” is checked out for editing by SHAREPOINT\\system.
Declare as inplace record
Ok, this could be painful if you want to do different things depending on the way the document has been locked.
How do we deal with this?

Get the Checked Out User

To get the checked out user you can simply look at the document properties on the library:

Checked Out To

This can be collected using the following REST API call:

GetCheckedOutUser
The checked out by user will contain SHAREPOINT\System when the document has been declared as a record.
CheckeOutInSharePoint
In SharePoint this is displayed as the System Account. With the above mentioned REST API call you will find that you get a user account returned with an ID 1073741823

Get the Locked By User

Getting the locked user can be done with a similar api call.
Get Locked User
Now that we can collect the locked by user and the checked out by user we have all the information that we need.
Now within your flows you can build in the logic to collect the locked and check out status of your documents in SharePoint.

Next Steps

Once you have identified your steps that are potentially affected by document locks, you might want to build retry steps in your flows. Please have a look at the Flow Retrying Pattern for more details

By Pieter Veenstra

Business Applications and Office Apps & Services Microsoft MVP working as a Microsoft Productivity Principal Consultant at HybrIT Services. You can contact me using contact@veenstra.me.uk.

11 thoughts on “Power Automate – Handling file locks in SharePoint”
  1. Great write-up!

    Could you provide more details on the api string in the image for GetFolderByServerRelativeUrl. There is a FX replace and a ServerRelativeUrl that are obscure.

    Thanks!

  2. “lockedByUser” http request (REST API) will not work correctly when multiple users edit the document (co-authoring).Http request will return only the UserPrincipalName of the first user editing the document. If the document has been closed or not in edit by first user and it is still open in edit (locked) by other users, the request will still return the first UserPrincipal as lockedbyuser. MS needs to fix this…

    1. Hi Ionel,

      I had a bit of a further look into this.

      I opened the same documents as two different users and as I was editing the document, my flow that tried to update properties and read who locked the document would give me details on the locking user.

      So far I found that the user who last edited/typed into/updated the document is the one that is reported as the locking user.

      This is how I expected it to be.

      When I was testing this however I found every now and then that things were slightly delayed. Also during my tests I used the Browser version of Word only. So I wonder if the Auto save of documents creates the confusion. So it might just be a matter that the lock happens on saving or on the start typing after a save or anything like that.

      I wonder what happens when two users indeed both type in a separate section of the document. Technically you should then have two users locking part of the document.

  3. It’s frustrating that there’s no administrative “unlock” method available to PowerAutomate Flow authors. Having a flow wait in a Do Until loop until the file unlocks is a horrid solution – considering that the unlock isn’t always triggered upon the user exiting their file!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.