Skip to content

Instantly share code, notes, and snippets.

@nlarkin
Created March 25, 2013 13:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nlarkin/5237244 to your computer and use it in GitHub Desktop.
Save nlarkin/5237244 to your computer and use it in GitHub Desktop.
Powershell Script to force a undocheckout of a specified document using the bypasslocks method.
cls
$error.Clear()
##################################################################################################
# Load SharePoint Snapin
##################################################################################################
$snap = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snap -eq $null) {
Write-Host "Loading Powershell Snapin..." -ForegroundColor Yellow
Add-PSSnapin Microsoft.SharePoint.Powershell
}
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null
$checkout = {
param( [Microsoft.SharePoint.SPListItem]$a )
$a.File.UndoCheckOut()
$a.File.Update()
$a.SystemUpdate($true)
}
function UnlockFile($item)
{
if ($item.File.CheckOutStatus -ne "None" )
{
[Microsoft.Office.RecordsManagement.RecordsRepository.Records]::bypasslocks($item, $checkout)
}
}
$url = "http://sharepointsite.com/site"
$fileurl = "http://sharepointsite.com/site/Documents/FullDocURL.pdf"
$web = Get-SPWeb $url
$item = $web.GetListItem($fileurl)
UnlockFile $item
$web.Dispose()
@Vinod35
Copy link

Vinod35 commented May 22, 2014

Hi,
When I try to use the script, I get below error,
Exception calling "BypassLocks" with "2" argument(s): "Attempted to perform an unauthorized operation."
On Line:
[Microsoft.Office.RecordsManagement.RecordsRepository.Records]::bypasslocks($item, $checkout)

Even though executing the script as administrator, I have Site Collection Administrator rights on the site. I am still getting this error.
Can you help me.

@kausarayub
Copy link

Thanks a lot.. I was stuck with this for 2days and finally this code helped 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment