I have a document library in a SP 2013 site. I have a document content type with a document template associated to it. Now, I have written an ItemUpdated event receiver which will update the document with some text (not the properties of the list item associated with the file for which I could use SystemUpdate method. So please keep this in mind, while providing the solution). Now, I go to the library, click on the files ribbon and then click New Document (not upload), this opens up my template and make some changes and save it (the document is open in my local machine). The event receiver fires, but when it tries to do SPFile.SaveBinary (or SPFiles.Add(with params, I tried both), it throws the error saying

The file 'fileUrl' is locked for shared use by "domainName\contributor1".

If, instead of saving the file, I close the file after making changes, this error doesn't occur. But I cannot control whether the user will save or close. I have not set the Require Checkout option also. Only versioning is turned on in the library. Also, I had turned on the DIP (Document Information Panel) display earlier, which I have now turned off. Even then, it is showing up.

Can anyone please help me with these issues?

3 Answers 3


Using C#

You must have wait for some time to check if the file is ready to use or not. Means you need to wait till lock status SPFile.SPLockType.None becomes true.

Please check below code which applies System.Threading.Thread.Sleep(2000) for file to unlock.

if (SPContext.Current == null)
          //If the initial file length is 0, pause the thread for 2 seconds
          if (properties.ListItem.File.Length == 0)


               //Since our item exists, run the GetItemById to instantiate a new  and updated SPListItem object 
               var spFile = properties.List.GetItemById(properties.ListItemId);

               //SharePoint places an Exclusive lock on the file while the data is being loaded into the file
               while (spFile.File.LockType != SPFile.SPLockType.None)
                       spFile = properties.List.GetItemById(properties.ListItemId);

                       //We need to check if the file exists, otherwise it will loop forever if someone decides to cancel the upload
                       if (!spFile.File.Exists)

                //If someone thought it was a good idea to actually load a 0 byte file, don't do anything else
                if (spFile.File.Length == 0)



You can unlock it programmatically!

There is no UI to unlock the locked files - as we do have for Check-in Checked-out files. So, Here is my solution to unlock the locked files using PowerShell.

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

#Variables for Web and File URLs
$WebURL ="http://intranet.crescent.com/support/"
$FileURL ="http://intranet.crescent.com/support/T1Support/Reports/ServiceTickets.xlsx" 

#Get Web and File Objects
$web = Get-SPWeb $WebURL
$File = $web.GetFile($FileURL)

#Check if File is locked
 if ($File.LockId -ne $null)
     Write-host "File is Loked out by:" $File.LockedByUser.LoginName
     Write-host "File Lock Type: "$file.LockType
     Write-host "File Locked On: "$file.LockedDate
     Write-host "File Lock Expires on: "$file.LockExpires

     #To Release the lock, Uncomment below lines:
     #Write-host "Released the lock!" 
  • Thanks. But I need a C# based solution, as it has to be automated. Also, when I check for the Lock property details of the SPFile, it is returning null / blank, which is essentially saying there is no lock. So it looks like a Client side lock.
    – Ven
    Jun 30, 2016 at 5:06
  • Ok I will update my answer asap... Happy to help you ....
    – Hardik
    Jun 30, 2016 at 5:09
  • I have updated my answer with c# code. Kindly check it & let me know.
    – Hardik
    Jun 30, 2016 at 7:26

Usually Windows SharePoint Services puts a write lock on the document on the server when a document is opened. The write lock times out after 10 minutes. Users cannot modify the document during the time when the document is locked. Workaround:

  1. Check whether the file has been properly checked in or not. If not, refresh the page and try to check in the file again.
  2. Delete the local copy of the file present in the cache folder of the concerned user account (access to user’s PC required).
  3. Close all the instances of file type opened in the system. E.g. if the file type is excel, then close all the excel files currently opened.
  4. Also kill the file type service (excel in this case) using the task manager

Sometimes a lock seems to persist, which may be the result of browser caching problems or bugs in the way client applications handle the file locks. The PowerShell for removing a file lock is as follows:

$web = get-spweb "site Url"
$list = $web.Lists["My Library"]
$item = $list.Items.GetItemById(123)

Reference: click here

  • There is no specific time to wait until lock is released as it can be automatically extended.
    – Hardik
    Jun 29, 2016 at 7:06
  • Thanks Sabitha. But I cannot control the above options, as I cannot go and tell every user explicitly to close the document. Also, PS is not an option for me. I need it automated.
    – Ven
    Jun 30, 2016 at 5:05

Finally I found the solution. If the file is open in any client machine in Edit mode, then there is a lock created on it. Though most of the Lock related properties of SPFile are of not much help, the LockId property helps. It carries a GUID as a string, which means that the file actually has a lock. So calling the ReleaseLock method on the SPFile by passing the SPFile.LockId as the argument after checking it is not empty, the file gets updated without throwing any error, even if it is open in any client machine. The following code helps. (file is the instance of SPFile)


But still, I am not able to figure out how to hide the Document Information Panel (DIP). Can anyone please suggest? In the Document Information Panel settings, the checkbox to show/hide DIP is already unchecked.

  • Thanks for posting the resolution here... It will help a lot..
    – Hardik
    Jul 1, 2016 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.