Friday, May 3, 2013

“Manage files which have no checked in version” in SharePoint 2010

The problem is if the library version setting is set  to “Require Check Out”  = yes and copied documents using explorer view



This means that all the files that were dropped into the library using explorer view would need to be checked in as there would be no current version.
To make matters worse, the user who dumped all the files left the company, hence there is no checked in version and all the files remain invisible to everyone in the site.
Generally for a scenario like this you could use the option ”Manage files which have no checked in version” from the library settings under Permissions and Management


Then from there you could take ownership of all the files, and check them in yourself.
But for our scenario the number of files dropped exceeded our list view threshold by a lot and we couldn’t even open the view “Manage files which have no checked in version“. However even if we could view all the files i.e. extending the threshold temporarily,  it would have been a real pain taking ownership of them all and checking them all in.

PowerShell Solution.
A short little set of PowerShell Functions that can take ownership of the files, then recursively check in all files that are being checked out by the system account.

#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
}


We can then run the script and use the Checkin-AllUnmanagedFiles function.

PS> Checkin-AllUnmanagedFiles -site "http://yoursite/" -library "Library Name"

For example I have a “Shared Documents” library which requires files be checked out for editing. I then dropped a few files into it’s explorer view



We can now see that these files show up in the library but are all checked out to myself hence no other user can see the file as there is no previous version yet.



Now if we run my script from the command shell:
(Note: I saved my script in a ps1 file called CheckinFiles.PS1)
PS> . .\CheckinFiles.PS1
PS> Checkin-AllUnmanagedFiles -site "http://torapd149:3434/subsite/" -library "Shared Documents" > .\checkin.txt

We can see all the files have now been checked in and modified by the System Account. Also I sent the output to a logfile called checkin.txt which tells which files were taking ownership of and which files were checked in.

1 comment:

  1. Had a problem with this script when the checkin part was running. Error was "checkin-AllFiles : Cannot bind argument to parameter 'currFolder' because it is null." Make sure that the URL for your library is the same as the library name. In our case the library had been renamed after it was created so the URL was different. Thanks for the great script!

    ReplyDelete