Wednesday, February 20, 2013

Find All Checked Out Files and Check-In them Back

I'm Sharing one of the PowerShell script I used in MOSS 2007 to SharePoint 2010 migration. Since its very difficult to check-in back all the checked-out files after migration, its a best practice to check-in all checked out files prior.

This PowerShell script will Scans, Generates Report and check-in all checked out files. Run this script with Farm Administrator privileges.

# For MOSS 2007 compatibility
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

#Region MOSS2007-CmdLets

Function Get-SPWebApplication()
{   
  Param( [Parameter(Mandatory=$true)] [string]$WebAppURL )
  return [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($WebAppURL)
}

Function global:Get-SPSite()
{
  Param( [Parameter(Mandatory=$true)] [string]$SiteCollURL )

   if($SiteCollURL -ne '')
    {
    return new-Object Microsoft.SharePoint.SPSite($SiteCollURL)
   }
}
 
Function global:Get-SPWeb()
{
 Param( [Parameter(Mandatory=$true)] [string]$SiteURL )
  $site = Get-SPSite($SiteURL)
        if($site -ne $null)
            {
               $web=$site.OpenWeb();
            }
    return $web
}
#EndRegion

 Function GetCheckedOutFiles()
 {  
    #Define 'Web Application URL' as Mandatory Parameter
    Param( [Parameter(Mandatory=$true)] [string]$WebAppURL )
 
 #Get the Web Application
    $WebApp=Get-SPWebApplication($WebAppURL)

    #Write the CSV Header - Tab Separated
 "Site Collection Name `t Site Name`t Library `t File Name `t File URL `t  Last Modified `t Checked-Out By" | out-file CheckedOutFiles.txt

 #Arry to Skip System Lists and Libraries
 $SystemLists =@("Converted Forms", "Master Page Gallery", "Customized Reports", "Form Templates",  
                 "List Template Gallery", "Theme Gallery", "Reporting Templates",  "Solution Gallery",
           "Style Library", "Web Part Gallery","Site Assets", "wfpub")
 
   #Loop through each site collection
  foreach($Site in $WebApp.Sites)
   {
    #Loop through each site in the site collection
     foreach($Web in $Site.AllWebs)
   {
            #Loop through each document library
            foreach ($List in $Web.GetListsOfType([Microsoft.SharePoint.SPBaseType]::DocumentLibrary))
            {
                #Get only Document Libraries & Exclude Hidden System libraries
                if ( ($List.Hidden -eq $false) -and ($SystemLists -notcontains $List.Title) )
                {
     #Loop through eadh Item
      foreach ($ListItem in $List.Items) 
      {
                  if( ($ListItem.File.CheckOutStatus -ne "None") -and ($ListItem.File.CheckedOutByUser -ne $null))
      {
                        #Log the data to a CSV file where versioning size > 0MB!
                        "$($Site.RootWeb.Title) `t $($Web.Title) `t $($List.Title) `t $($ListItem.Name) `t $($Web.Url)/$($ListItem.Url) `t  $($ListItem['Modified'].ToString()) `t  $($ListItem.File.CheckedOutByUser)" | Out-File CheckedOutFiles.txt -Append
      
       #*** Uncomment the below line to Check-in****#
       $ListItem.File.Checkin("Checked in by Administrator")
      }
                    }
                }
            }
   $Web.Dispose()          
        }
 $Site.Dispose()          
    }
 
    #Send message to output console
    write-host "Checked out Files Report Generated Successfully!"
}

#Call the Function to Get Checked-Out Files
GetCheckedOutFiles "http://sharepoint.company.com"

Find all Checked-Out files from a SharePoint Library using PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Variables
$WebURL="http://sharepoint.crescent.com"
$LibraryName="Shared Documents"

#Get Objects
$Web = Get-SPWeb $WebURL
$Folder = $web.GetFolder($LibraryName)

#Function to find all checked out files in a SharePoint library
Function Get-CheckedOutFiles($Folder)
{

    $Folder.Files | Where { $_.CheckOutStatus -ne "None" }  | ForEach-Object {
    write-host ($_.Name,$_.URL,$_.CheckedOutBy)

    #To Check in
    #$_.Checkin("Checked in by Administrator")
    }

   #Process all sub folders
   $Folder.SubFolders | ForEach-Object {
   Get-CheckedOutFiles $_
   }
}

#Call the function to find checkedout files
Get-CheckedOutFiles $Folder 

Its also possible to retrieve all checked out files via Search! Here is how: Create a Managed Property by adding the existing crawled property: ows_CheckoutUser(Text), then you can use that managed property in your search!
Bulk Check-In all checked-out files in Site collection:
If you want to check-in all checked-out files by you, use "Content and Structure" link under site settings. (/_Layouts/sitemanager.aspx)
Bulk Check-in Files in SharePoint
You have the option "Checked Out to Me" which lists all files checked-out to you. Just select the desired files and click Check-in from actions menu.



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

5 comments :

  1. Hi
    thanks for the information but it helps to checkout all files while doing migration from 2007 to 2010.
    my situation is in Sp 2010 user had left the organization and we have only one document which was checked out bye that user.can you explain this scenario for me with clear examples because i don't have scripting knowledge.

    ReplyDelete
    Replies
    1. Surya,

      Sure, just change this line in the script. From:
      if( ($ListItem.File.CheckOutStatus -ne "None") -and ($ListItem.File.CheckedOutByUser -ne $null))

      To:
      if( ($ListItem.File.CheckOutStatus -ne "None") -and ($ListItem.File.CheckedOutByUser.LoginName -eq "Domain\UserName" ))

      Delete
  2. It appears to run but doesn't. It creates the text file and creates the headers for it. And it seems to be processing something but it never finds any checked out files and I know I have a ton of them. Not sure what I am missing

    ReplyDelete
    Replies
    1. Make sure you are running this script from FARM Admin Credentials, which should be granted with "FULL Control" User Policy to your target web application.

      Delete
    2. Getting the same problem. Tried it with both a farm admin account (my own) and the actual farm installation account, and both have the same result. Script appears to run for a few minutes then outputs a report with only the headers.

      Delete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...