• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Tachytelic.net

  • Sco Openserver
    • Sco Unix Support
    • SCO Openserver Installation ISOs
    • SCO Openserver Binaries
    • Add a Hard Drive to an Openserver 5 Virtual Machine
    • Install SCO Vision SQL-Retriever ODBC Driver on Windows 10
    • License Expired on Virtual SCO Openserver Installation
    • How to reset the root password on SCO Openserver 5
  • Scripting
    • PowerShell
      • Add leading zeros
      • Check if a File Exists
      • Grep with Powershell
      • Create Environment Variables
      • Test for open Ports
      • Append to a Text File
    • VBScript
      • Check if a File Exists
      • lpad and rpad functions
      • Windows Update E-Mail Notification
  • Office 365
    • Connect to Office 365 with PowerShell
    • Add or remove an email alias using Powershell
    • Change Primary email address of Active Directory user
    • How to hide an AD user from the Global Address List
    • How to hide mail contacts from the Global Address List
    • Change the primary email address for an account with PowerShell
    • Change Primary email address of an AD User
    • Grant a single user access to access to all calendars
    • Forward email to an external address using Powershell
    • Convert shared mailbox to user mailbox with Powershell
  • Get in Touch
  • About Me
    • Privacy Policy

Working with locked documents in Power Automate Flows

November 21, 2020 by Paulie 1 Comment

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:

Image showing a Microsoft Power Automate Error condition when trying to update a locked file.

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:

Image of a Power Automate Flow that deals with a file that is locked by another user.

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:
    Image of a set variable action that determines if a file is locked during a Power Automate flow
    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.

Filed Under: Power Platform Tagged With: Power Automate

Reader Interactions

Comments

  1. soumya says

    January 7, 2021 at 9:38 am

    hi , Do you know if we can run powershell scripts in powerapps

Leave a Reply Cancel reply

Primary Sidebar

Power Automate Support Image
Link to my LinkedIn Profile

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 107 other subscribers.

Go to mobile version