Thursday, September 26, 2013

How To Chekin Mutiple Files or Checkin All undamaged Files using Powershell

Step 1: create a Function

===============================
#Add SharePoint Snapin if not using SharePoint's PowerShell Console
if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PsSnapin Microsoft.SharePoint.PowerShell
}
#First function takes ownerhip of all unmanaged files with no version
function Take-Ownership{
param(
$spSite,
$spList
)
$site = Get-SPWeb -Identity $spSite
$list = $site.Lists[$spList]

$list.CheckedOutFiles | ForEach-Object{
$_.LeafName+ " had it's ownership taken from: "+$_.CheckedOutBy.DisplayName+"`n"
$_.TakeOverCheckOut()
}
}
#This Function takes a folder object and recursively checks in
#all files which are checked out by the System Account
function checkin-AllFiles{
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[Microsoft.SharePoint.SPFolder]
$currFolder
)

$currFolder.Files | ForEach-Object {
if ($_.CheckedOutByUser.UserLogin -eq "SHAREPOINT\system"){
$_.CheckIn("File checked in by administrator")
$_.Name+" has been checked in`n"
}
}
$currFolder.SubFolders| ForEach-Object{
checkin-AllFiles -currFolder $_
}
}
#This function basically combines the previous 2 for primary use
function Checkin-AllUnmanagedFiles{
param(
$site,
$library
)
$spSite = Get-SPWeb -identity $site
$spFolder = $spSite.Folders[$library]

Get-Date
"Taking ownership of the following Files`n"
Take-Ownership -spSite $site -spList $library
"-----------------------------------------------------------------------------`n"

    Get-Date
"Checking in the following files`n"
checkin-AllFiles -currFolder $spFolder
Get-Date
}
================================

Step 2: Call the Function to do the work

Checkin-AllUnmanagedFiles -site http://mysite/sites/demant -library shared documents >>c:/checkedin.txt

No comments:

Post a Comment