How to Get a List of Checked out Files– SharePoint PowerShell

Sathish Nadarajan
 
Solution Architect
June 7, 2016
 
Rate this article
 
Views
11668

In this article, let us see, how to retrieve the List of Files which are checked out in SharePoint

 
 ################ This method will return the Checked Out files 
 function GetCheckedOutFiles()
 {
     Write-Host "Entered GetCheckedOutFiles Method" -ForegroundColor Yellow 
     Add-Content "$ProgressFile" "Entered GetCheckedOutFiles Method"
     $script:Progress = "9:Entered"
     
     # Get the Web Application Object    
     $WebApplication = Get-SPWebApplication $Config.Configuration.WebApplication.URL
 
     # Assign the XML Output File Paths
     $CheckedOutFiles_XML_Path = $scriptBase + "Reports-$LogTimeCheckedOutFiles.xml"
     
     # Create the XML File Tags
     
     $xmlWriter = New-Object System.XMl.XmlTextWriter($CheckedOutFiles_XML_Path,$Null)
     $xmlWriter.Formatting = 'Indented'
     $xmlWriter.Indentation = 1
     $XmlWriter.IndentChar = "`t"
     $xmlWriter.WriteStartDocument()
     $xmlWriter.WriteComment('Files List On the WebApplication ' + $WebApplication.DisplayName)
     $xmlWriter.WriteStartElement('WebApplication')
     $xmlWriter.WriteEndElement()
     $xmlWriter.WriteEndDocument()
     $xmlWriter.Flush()
     $xmlWriter.Close()
     
     # Get theSiteCollections
     $SiteCollections =  $WebApplication | Get-SPSite -Limit All
     #$SiteCollections =  Get-SPSite "http://ctsc00556722901:5555/sites/TeamSite2/"
     
     # write the output on XML File
     $xmlDoc = [System.Xml.XmlDocument](Get-Content $CheckedOutFiles_XML_Path);
     $siteCollectionNode = $xmlDoc.CreateElement("SiteCollections")
     $xmlDoc.SelectSingleNode("//WebApplication").AppendChild($siteCollectionNode)
     $xmlDoc.Save($CheckedOutFiles_XML_Path)
     
     # Iterate through the Site Collection
     foreach($SiteCollection in $SiteCollections)
     {
         $siteCollectionName = $SiteCollection | select @{label = "Title";Ex = {$_.rootweb.Title}} 
         
         # write the output on XML File
         $xmlDoc = [System.Xml.XmlDocument](Get-Content $CheckedOutFiles_XML_Path);
         $siteCollectionNode = $xmlDoc.CreateElement("SiteCollection")
         $xmlDoc.SelectSingleNode("//WebApplication/SiteCollections").AppendChild($siteCollectionNode)
         $siteCollectionNode.SetAttribute("Name", $siteCollectionName.Title)
         
         $subSitesNode = $siteCollectionNode.AppendChild($xmlDoc.CreateElement("SubSites"));
         $xmlDoc.Save($CheckedOutFiles_XML_Path)
         
         # Get the Sub Webs
         $Webs = Get-SPWeb -site $SiteCollection -Limit All 
         
         # Iterate through the Webs
         foreach($Web in $Webs)
         {
             # write the output on XML File
             
             $subSiteNameNode = $subSitesNode.AppendChild($xmlDoc.CreateElement("SubSite"));
             $subSiteNameNode.SetAttribute("Name", $Web.Title)
             $subSiteNameNode.SetAttribute("Url", $Web.Url)
             $subSiteNameNode.SetAttribute("WebID", $Web.Id)
             $parentWebTitle = ""
                             
             if($Web.ParentWebID -ne "00000000-0000-0000-0000-000000000000")
             {
                 $parentWeb = $SiteCollection.OpenWeb($Web.ParentWebID)
                 $parentWebTitle = $parentWeb.Title
                 
             }
             else
             {
                 $parentWebTitle = "RootWeb"
             }    
             $subSiteNameNode.SetAttribute("ParentWebName", $parentWebTitle)
             $subSiteNameNode.SetAttribute("ParentWebID", $Web.ParentWebID)
                             
             $FilesNode = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("Files"));
             
             $xmlDoc.Save($CheckedOutFiles_XML_Path)
             
             # Iterate through the Lists
             foreach ($list in $Web.Lists) 
             {
                 # Check for Document Library
                 if ($list.BaseType -eq “DocumentLibrary”) 
                 {
                     # Iterate through the Items
                     foreach ($item in $list.Items) 
                     {
                         # Check for Checked Out Status
                         if($item.File.CheckOutStatus -ne "None")
                         {
                             $FileNode = $FilesNode.AppendChild($xmlDoc.CreateElement("File"));
                             $FileNode.SetAttribute("Name", $Item.File.Title)
                             $FileNode.SetAttribute("Url", $Item.File.Url)
                             $FileNode.SetAttribute("CheckedOutBy", $item.File.CheckedOutBy)
                             $FileNode.SetAttribute("ServerRelativeUrl", $item.File.ServerRelativeUrl)
                             $FileNode.SetAttribute("List", $list.Title)
                                                             
                             $xmlDoc.Save($CheckedOutFiles_XML_Path)
                         }
                     }
                 }
             } 
         }
     }    
     # write the output on XML File
     $xmlDoc.Save($CheckedOutFiles_XML_Path)
     
     Write-Host "Completed GetCheckedOutFiles Method" -ForegroundColor Yellow 
     Add-Content "$ProgressFile" "Completed GetCheckedOutFiles Method"
     $script:Progress = "9:Success"
 }
 ####### End of Method #################################
 
 
 

The snippet is self-explanatory.

 

Happy Coding,

Sathish Nadarajan.

Category : PowerShell, SharePoint

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment