Update a Locked File property

Recently I ran into a HTTP 423 Locked error message while trying to update a SharePoint Online file property via a Power Automate flow. This article explains how I worked around this by using the sharedLockId property in a validateUpdateListItem HTTP request.

Setting the Scene

In my setup I was dealing with an Approval process. A person was being asked to peer review a document in a SharePoint document library via an approval task. After the approval was completed a couple of properties of the document would be updated, via a Update file properties action. A pretty common scenario.

Below is an example setup of such a Update file properties action.

updatefileproperties

However, after reading the document that person didn’t close the document and the flow was triggered and the below error occurred. A HTTP 400 Bad Request: The file “Expenses.docx” is locked for shared use by dennis@contoso.onmicrosoft.com

http400_badrequest

A lot of Patterns

This is a very common error and a lot of workarounds and patterns have been shared in the past. Typically you see two type of approaches for handling this error:
Retry your actions until lock is released. Power Automate locked document by Paul Murana, SharePoint file locks Power Automate by Pieter Veenstra and Locked file checking pattern in Power Automate by Tomasz Poszytek are great examples of that.
Use a REST API call with the ValidateUpdateListItem method to update the property. System Update SharePoint Power Automate by Pieter Veenstra and Power Automate options to get round locked files by Reshmee Auckloo show for example how you can use that approach.

HTTP 423 Locked

I went for the second option, I decided to use the validateUpdateListItem method.

Below is an example setup of such a method in a Send an HTTP request to SharePoint action.

validateupdatelistitem_properties

However, to my surprise that generated a ‘new’ error, in this case: a HTTP 423 Locked: The file “Expenses.docx” is locked for shared use by dennis@contoso.onmicrosoft.com

http423_filelockedforshareduse

sharedLockId

After some research I came across this StackOverflow thread about SharePoint REST API validateUpdateListItem fails and returns a 423 locked error and this related GitHub issue about validateUpdateListItem does not work after coAuthoring was enabled.

Daniel Linden discovered that you could use a sharedLockId property in the payload of the validateUpdateListItem method. In those threads it was related to Pages, but I figured this might work for files as well. Kudos to him for sharing these findings.

vti_x005f_sourcecontrollockid

Like mentioned in those threads the challenge however was to actually dynamically retrieve that lock id value. After a bit of exploring of several methods I managed to find the vti_x005f_sourcecontrollockid in the File properties.

Flow Setup

Based on these findings I decided to use the below setup. If you want to give it a name, call it the sharedLockId pattern to handle updates to locked files.

sharedlockidpatterntohandlelockedfiles

1. Add a Send an HTTP request to SharePoint action.
This first request is used to return the lockedByUser property value.

lockedbyuser

a. Select your preferred site in the Site Address
b. Use the GET method
c. Use the URI from the code snippet below

d. Use the Headers from the code snippet below

e. Leave the Body field empty

2. Add a Condition action.
The condition is used to check if the lockedByUser property contains a value

condition_odatanull

a. Use the expression below for the left value in the condition

b. Use the is not equal to true operator
c. Add the true function as the right value.

3. Add a second Send an HTTP request to SharePoint action (in the If Yes section).
This second request is used to return the vti_x005f_sourcecontrollockid property value.

sharedlockid_property

a. Select your preferred site in the Site Address
b. Use the GET method
c. Use the URI from the code snippet below

d. Use the Headers from the code snippet below

e. Leave the Body field empty

4. Add a third Send an HTTP request to SharePoint action (in the If Yes section).
This third request is used to update the file property.

validateupdatelistitem_withsharedlockid

a. Select your preferred site in the Site Address
b. Use the POST method
c. Use the URI from the code snippet below

d. Use the Headers from the code snippet below

e. Use the Body from the code snippet below

4. Add a fourth Send an HTTP request to SharePoint action (in the If No section).
This fourth request is used to update the file property but doesn’t include the sharedLockId property.

validateupdatelistitem

a. Select your preferred site in the Site Address
b. Use the POST method
c. Use the URI from the code snippet below

d. Use the Headers from the code snippet below

e. Use the Body from the code snippet below

That is it for the cloud flow setup of this example. Hopefully it is useful to handle locked files. Let me know in the comments what you think of this approach?

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

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