Handling file locks in SharePoint

Microsoft Flow – Handling file locks in SharePoint

Have you ever struggled to handle files in SharePoint when you are running your flows? Microsoft Flow’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 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

Leave a Reply

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

%d bloggers like this: