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:
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.
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:
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
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)