Get the Files with Version Greater than the Given Version Number – SharePoint PowerShell

Sathish Nadarajan
 
Solution Architect
June 2, 2016
 
Rate this article
 
Views
5330

In this article, let us see, how to retrieve the list of Files with Version Greater than the Given Version Number. i.e., on the Config file, I will have some version number and within the WebApplication, the files with greater than the given version needs to be listed on the XML File.

 ######################### this method will return the Files with Greater Version than the Given Input
 
 function GetFilesWithVersionGreaterThanGiven()
 {
     Write-Host "Entered GetFilesWithVersionGreaterThanGiven Method" -ForegroundColor Yellow 
     Add-Content "$ProgressFile" "Entered GetFilesWithVersionGreaterThanGiven Method"
     $script:Progress = "5:Entered"
     
     # Get the WebApplication Object
     $WebApplication = Get-SPWebApplication $Config.Configuration.WebApplication.URL
 
     # Assign the XML Output File Paths
     $FilesWithVersionGreaterThanGiven_XML_Path = $scriptBase + "Reports-$LogTimeFilesWithVersionGreaterThanGiven.xml"
     
     # Create the XML File Tags
     
     $xmlWriter = New-Object System.XMl.XmlTextWriter($FilesWithVersionGreaterThanGiven_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 All SiteCollections within webApplication
     $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 $FilesWithVersionGreaterThanGiven_XML_Path);
     $siteCollectionNode = $xmlDoc.CreateElement("SiteCollections")
     $xmlDoc.SelectSingleNode("//WebApplication").AppendChild($siteCollectionNode)
     $xmlDoc.Save($FilesWithVersionGreaterThanGiven_XML_Path)
     
     # Iterate through the Site Collections
     foreach($SiteCollection in $SiteCollections)
     {
         $siteCollectionName = $SiteCollection | select @{label = "Title";Ex = {$_.rootweb.Title}} 
         
         # write the output on XML File
         
         $xmlDoc = [System.Xml.XmlDocument](Get-Content $FilesWithVersionGreaterThanGiven_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($FilesWithVersionGreaterThanGiven_XML_Path)
         
         # Get All the 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($FilesWithVersionGreaterThanGiven_XML_Path)
             
             # Iterate through the Lists
             foreach ($list in $Web.Lists) 
             {
                 # validate for Document Library
                 if ($list.BaseType -eq “DocumentLibrary”) 
                 {
                     # Iterate threough the Doc Library Items
                     foreach ($item in $list.Items) 
                     {
                         # Check for the Version
                         if([decimal]$item.File.UIVersionLabel -ge [decimal]$Config.Configuration.FetchFilesAboveTheVersion)
                         {
                             # If Yes, then write the details on the XML Output file
                             
                             $FileNode = $FilesNode.AppendChild($xmlDoc.CreateElement("File"));
                             $FileNode.SetAttribute("Name", $Item.File.Title)
                             $FileNode.SetAttribute("Url", $Item.File.Url)
                             $FileNode.SetAttribute("Version", $item.File.UIVersionLabel)
                             
                             $xmlDoc.Save($FilesWithVersionGreaterThanGiven_XML_Path)
                                                                               
                         }
                     }
                 }
             } 
         }
     }    
     # write the output on XML File
     $xmlDoc.Save($FilesWithVersionGreaterThanGiven_XML_Path)
     
     Write-Host "Completed GetFilesWithVersionGreaterThanGiven Method" -ForegroundColor Yellow 
     Add-Content "$ProgressFile" "Completed GetFilesWithVersionGreaterThanGiven Method"
     $script:Progress = "5: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