SharePoint Pals
 | Sign In
Get the Files with Version Greater than the Given Version Number – SharePoint PowerShell
by Sathish Nadarajan 2 Jun 2016
Author
SharePoint MVP
Visits   
Today  :   3     Total  :    3026

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-$LogTime\FilesWithVersionGreaterThanGiven.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.

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Note: For Customization and Configuration, CheckOutRecent Tweets Documentation

Tags