Remove SharePoint Client Side File Lock

Occasionally I have a user that leaves a file open on their client PC and locks the file for editing.  These locks stop anyone else from updating the document.  This emulates the behavior of a file in a Windows file system, but since SharePoint isn’t a true file system this is handled in the SharePoint content database.

SharePoint handles these file locks as “Short Term” checkouts on the file.  In PowerShell you can see this type of lock in the file property called “CheckOutType”.  The following script would display the checkout status of a file with the ID of “1” inside the “Documents” library in the site located at http://sharepoint/sites/web1

$w = Get-SPWeb "http://sharepoint/sites/web1"
$l = $w.Lists["Documents"]
$i = $l.GetItemById(1)
Write-Host $i.File.CheckOutStatus

The output would be “ShortTerm” for a file locked for editing by the client.  It would be “LongTerm” if someone checked the file out.  It may be advantageous to be able to override these “Short Term” locks in cases such as when a specific user locks an important file by opening it in Word, but then leaves on vacation.  Since there is no “Check Out” according to the SharePoint user interface there is no easy way for someone to do this.

Luckily there is a way to release this lock from PowerShell.  The script below opens the site while impersonating the user with the lock and then releases the lock their client established.  It accepts three parameters: the url of the site, the name of the list, and the id of the item that is locked.  This script will effectually pull the rug out from under the client and different applications may handle this better than others.  Use good judgment when deciding when this script is appropriate.

param (
$w = get-spweb $url
$l = $w.lists[$list]
$i = $l.GetItemById($item)
$s = New-Object Microsoft.SharePoint.SPSite($, $i.File.LockedByUser.UserToken)
$w = $s.OpenWeb($
$l = $w.lists[$list]
$i = $l.GetItemById($item)
Tagged with: ,
Posted in PowerShell
2 comments on “Remove SharePoint Client Side File Lock
  1. simpiligno says:

    Just a heads up, you have a stray single quote in the New-Object line of the script. Its in between the Site.ID and the UserToken parameters.

  2. Thomas Gass says:

    This is fantastic, we’ve had a file for ages on one of our clients that keeps getting this error and have always had to download, delete and reupload. Nice work.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: