Tags

,

One day, I got a request from a colleague to bulk check in files he has checked out. He also pointed me to this great post:

http://sharepoint.stackexchange.com/questions/103610/how-to-checkin-all-checked-out-files-via-powershell

Thanks to Aanchal for this helpful post above.

However, as I often write, I work in a hurry in-between interruptions. Between phone calls and bosses stopping by. My colleague had the files scattered all over many folders and subfolders. Please note the following line:

$getFolder = $spWeb.GetFolder(“Shared Documents”)

If I run the above, I will barely touch the surface. Because all the files are distributed in unpredictable folders. On top of that, these were first time checkins. So
$_.CheckOutStatus -ne “None” does not quite catch these files. I however found that if you get the list and look for CheckedOutFiles, these files are captured. And then I used TakeOverCheckOut() for checking these files to me.
Here is the script, that worked for me:

Add-PSSnapin Microsoft.SharePoint.PowerShell

function CheckInDocument([string]$url, [string]$libName)
{

$spWeb = Get-SPWeb $url
$getLib = $spWeb.Lists.TryGetList($libName)

$getLib.CheckedOutFiles | ?{ $_.CheckedOutBy.UserLogin.ToLower() -match “Admin’s Network Id”.ToLower()} | %{

$myCheckedOutFile = $_

$myExactFolderName = $myCheckedOutFile.DirName.SubString(13) //This is //to get the exact folder path without the /sites/sitename, this number may //be different for you

$getFolder = $spWeb.GetFolder($myExactFolderName)

$getFolder.Files | Where { $_.CheckOutStatus -ne “None” } | ForEach {
Write-Host “$($_.Name) is Checked out To: $($_.CheckedOutBy)”
$_.CheckIn(“Checked In By Administrator”)
Write-Host “$($_.Name) Checked In” -ForeGroundColor Green
}
}

$spWeb.Dispose()
}

function CheckOuttoAdmin([string]$url, [string]$libName)
{
$spWeb = Get-SPWeb $url
$getLib = $spWeb.Lists.TryGetList($libName)

$getLib.CheckedOutFiles | ?{ $_.CheckedOutBy.UserLogin.ToLower() -match “mycolleague’sNetworkid”.ToLower()} | %{
Write-Host “Taking over Checkout $($_.LeafName)”
$myFile = $_
$myFile.TakeOverCheckOut()
}

$spWeb.Dispose()

}

CheckOuttoAdmin “Your Site Collection URL” “Shared Documents”

CheckInDocument “Your Site Collection URL” “Shared Documents”

Advertisements