Saturday, January 26, 2013

Power-shell to check-in files

Scenario:
During bulk migration, lot of files left checked out because of lack of metadata. We wanted to check-in all these files. Also we want to restrict the script to only "Record Libraries" in record center.

Solution:
Power-shell

Code:

Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.Powershell

$rand = new-object System.Random

function CheckInDocument([string]$url)
{

$spWeb = Get-SPWeb $url
$SPBaseTypeDocumentLibrary = [Microsoft.SharePoint.SPBaseType]::DocumentLibrary
$lists = $spWeb.GetListsOfType($SPBaseTypeDocumentLibrary);

foreach ($list in $lists)
{
    if ($list.Hidden -eq $False -and $list.BaseTemplate.ToString() -eq "1302")
    {
        Write-Host Checking in documents from Library : $list.Title
        $getFolder = $spWeb.GetFolder($list.Title)
        $files = $list.CheckedOutFiles

        write-host "Total Checked Out Files : " $files.Count

        $list.CheckedOutFiles | Where { $_.CheckOutStatus -ne "None" } |
        ForEach {
            $_.TakeOverCheckOut();
            #$docItem = $list.GetItemById($_.ListItemId);
            #$docItem["Field To Update"] = "Some value";
            $docItem.SystemUpdate();                    
            $docItem.File.CheckIn("Checked In By Administrator");
            Write-Host "$($docItem.File.Name) Checked In" -ForeGroundColor Green

            }
    }
}

$spWeb.Dispose()


}

CheckInDocument http://sp2010
Article:

3 comments:

Anonymous,  January 31, 2013 at 3:26 AM  

Thank you very much. Was help me a lot.I have been looking everywhere for this post. :-)

Darvesh March 14, 2016 at 6:56 AM  

how to use it?

which variables to change? where do I specify the URL - sorry but never used SP or Power Shell :(

Sandeep March 14, 2016 at 7:44 AM  

Change you site collection Url in last line of script