Tags

,

Last afternoon, my boss came over and told a very important user is reporting he cannot check out a file from SharePoint as it is locked by another user.

So I noted the file’s location and told him I will start looking. And I banged my head and scolded me for not saving note from the last time when I resolved this. This is the deadly error when the file is not really checked out, but it is locked. Oops. What to do?

After googling the Bing a lot, these are the useful sites that came up:

http://sharepoint-community.net/profiles/blogs/sharepoint-2013-multi-authoring-this-file-is-locked-for-editing

http://www.techtimepiece.com/2012/04/sharepoint-error-file-is-locked-for.html

http://mysharepointt.blogspot.com/2012/05/file-name-is-locked-for-editing-by.html

http://livinghitech.tumblr.com/post/54437075583/sharepoint-file-locked-for-editing

http://www.codeproject.com/Articles/251161/Resolve-SharePoint-Document-Locked-for-Editing-Iss

http://blog.ftechsoftware.com/blog/post/2011/09/06/Resolve-SharePoint-Document-Locked-for-Editing-Issue.aspx

But in our case the problem was we did not have access to the user. He was under the weather and gone. So I had to do it without the user.

This wait for 10 minute did not work for us at all.
https://support.microsoft.com/en-us/kb/899709


I am an admin and I have access to servers and PowerShell. This Back in the Hack post is the key to resolving this problem. Thanks to gavinmckay:
https://gavinmckay.wordpress.com/2015/04/10/howto-remove-the-lock-on-a-sharepoint-file/

The thing is I was in a hurry. Also this was in production and I did not have time to test and run this entire script. So I had to get this done via few commandlets.
Note that in this post there is a line which holds the key of the resolution. The line is
$impFile.ReleaseLock($impFile.LockId)

When I tried the above line, it bombed. That is because this was being tried with the admin context. It’s very important that you open the site as the context of the user to whom the file seems to be locked. Check out this line:

$impSite= New-Object Microsoft.SharePoint.SPSite($web.Url, $user.UserToken)

Here are the commandlets that were enough for me to do the release of the lock:

— Get the user

$myWeb = Get-SPWeb http://yoursiteurl (no pages/default.aspx)

$myFile = $myWeb.GetFile(“Shared Documents/****/****/your file name.xlsx”)

$myUserID = $myFile.LockedByUser.ID

$myUser = $myWeb.AllUsers.GetByID($myUserID)

— The following is the all-important piece, open the site as the user who locked the file and then release the lock

$impSite= New-Object Microsoft.SharePoint.SPSite($myWeb.Url, $myUser.UserToken);

$impWeb = $impSite.OpenWeb()

$fileURL = “Shared Documents/****/****/your file name.xlsx”

$impFile = $impweb.GetFile($fileURL)

$impFile.ReleaseLock($myFile.LockId)

Advertisements