SharePoint Pals
 | Sign In
Get the List of WebParts in a WebApplication – SharePoint PowerShell
by Sathish Nadarajan 1 Jun 2016
Author
SharePoint MVP
Visits   
Today  :   3     Total  :    3461

In this article, let us see, how to get the list of WebParts in an XML File.

 
 ######################### this method will return the WebParts in the Web Application and the Page Information
  
 function GetWebParts()
 {
     Write-Host "Entered GetWebParts Method" -ForegroundColor Yellow 
     Add-Content "$ProgressFile" "Entered GetWebParts Method"
     $script:Progress = "4:Entered"
     
     # Get Web Application
     $WebApplication = Get-SPWebApplication $Config.Configuration.WebApplication.URL
 
     # Assign the XML Output File Paths
     $WebParts_XML_Path = $scriptBase + "\Reports-$LogTime\WebParts.xml"
     
     # Create the XML File Tags
     
     $xmlWriter = New-Object System.XMl.XmlTextWriter($WebParts_XML_Path,$Null)
     $xmlWriter.Formatting = 'Indented'
     $xmlWriter.Indentation = 1
     $XmlWriter.IndentChar = "`t"
     $xmlWriter.WriteStartDocument()
     $xmlWriter.WriteComment('WebParts List On the WebApplication ' + $WebApplication.DisplayName)
     $xmlWriter.WriteStartElement('WebApplication')
     $xmlWriter.WriteEndElement()
     $xmlWriter.WriteEndDocument()
     $xmlWriter.Flush()
     $xmlWriter.Close()
     
     # Get the site Collections Object 
     $SiteCollections =  $WebApplication | Get-SPSite -Limit All
     
     # write the output on XML File
     $xmlDoc = [System.Xml.XmlDocument](Get-Content $WebParts_XML_Path);
     $siteCollectionNode = $xmlDoc.CreateElement("SiteCollections")
     $xmlDoc.SelectSingleNode("//WebApplication").AppendChild($siteCollectionNode)
     $xmlDoc.Save($WebParts_XML_Path)
      
     # Iterate through the SiteCollections
     foreach($SiteCollection in $SiteCollections)
     {
         $siteCollectionName = $SiteCollection | select @{label = "Title";Ex = {$_.rootweb.Title}} 
         
         # write the output on XML File
         $xmlDoc = [System.Xml.XmlDocument](Get-Content $WebParts_XML_Path);
         $siteCollectionNode = $xmlDoc.CreateElement("SiteCollection")
         $xmlDoc.SelectSingleNode("//WebApplication/SiteCollections").AppendChild($siteCollectionNode)
         $siteCollectionNode.SetAttribute("Name", $siteCollectionName.Title)
         $siteCollectionNode.SetAttribute("Url", $SiteCollection.Url)
         
         $subSitesNode = $siteCollectionNode.AppendChild($xmlDoc.CreateElement("SubSites"));
         $xmlDoc.Save($WebParts_XML_Path)
         
         # Get All SubWebs
         $Webs = Get-SPWeb -site $SiteCollection -Limit All 
         
         # Iterate through the SubWebs
         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)
     
     
             $webPartsNode = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("WebParts"));
             
             $xmlDoc.Save($WebParts_XML_Path)
             
             # Check For Publishing Site
             if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($Web))  # If the Current Web is Publishing Web, then proceed
             {
                 # Get the Publishing Web Object
                 $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
                 
                 # Get the Pages Doc Library
                 $pages = $pWeb.PagesList
                 
                 # Iterate through all the Pages  
                 foreach ($item in $pages.Items) 
                 {
                     $fileUrl = $Web.Url + "/" + $item.File.Url
                     
                     # Get the webPartManager Object
                     $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
                     
                     # Get the WebParts
                     $wps = $manager.webparts
                     $wps | select-object @{Expression={$WebApplication.DisplayName};Label="WebApplication"},@{Expression={$siteCollectionName.Title};Label="SiteCollection"}, @{Expression={$Web.Title};Label="SubSite"},@{Expression={$item.File.Name};Label="Page Name"},@{Expression={$fileUrl};Label="Page URL"}, DisplayTitle, ID, IsVisible, @{Expression={$_.GetType().ToString()};Label="Type"}
                     
                     # Iterate through the WebParts
                     foreach($wp in $wps)
                     {
                         # write the output on XML File
                          
                         $webPartNodeNode = $webPartsNode.AppendChild($xmlDoc.CreateElement("WebPart"));
                         $webPartNodeNode.SetAttribute("Name", $wp.DisplayTitle)
                         $webPartNodeNode.SetAttribute("ID", $wp.ID)
                         $webPartNodeNode.SetAttribute("Visible", $wp.IsVisible)
                         $webPartNodeNode.SetAttribute("Type", $wp.GetType())
                         $webPartNodeNode.SetAttribute("PageName", $item.File.Name)
                         $webPartNodeNode.SetAttribute("FileURL", $fileUrl)
                         $xmlDoc.Save($WebParts_XML_Path)
                          
                     }
                 }            
             }
             else # If the Current Web is not a Publishing Web
             {
                 $pages = $null
                 # Get the Site Pages Doc Lib
                 $pages = $Web.Lists["Site Pages"]
                 
                 if ($pages) 
                 {
                     # Iterate through all the Pages
                     foreach ($item in $pages.Items) 
                     {
                         $fileUrl = $Web.Url + "/" + $item.File.Url
                         
                         # Get the WebPart Manager Object
                         $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
                         
                         # Get All the WebParts
                         $wps = $manager.webparts
                         $wps | select-object @{Expression={$WebApplication.DisplayName};Label="WebApplication"},@{Expression={$siteCollectionName.Title};Label="SiteCollection"}, @{Expression={$Web.Title};Label="SubSite"},@{Expression={$item.File.Name};Label="PageName"},@{Expression={$fileUrl};Label="Page URL"}, DisplayTitle, ID, IsVisible, @{Expression={$_.GetType().ToString()};Label="Type"}
                         
                         # iterate through the WebParts                                                
                         foreach($wp in $wps)
                         {
                             # write the output on XML File
                              
                             $webPartNodeNode = $webPartsNode.AppendChild($xmlDoc.CreateElement("WebPart"));
                             $webPartNodeNode.SetAttribute("Name", $wp.DisplayTitle)
                             $webPartNodeNode.SetAttribute("ID", $wp.ID)
                             $webPartNodeNode.SetAttribute("Visible", $wp.IsVisible)
                             $webPartNodeNode.SetAttribute("Type", $wp.GetType())
                             $webPartNodeNode.SetAttribute("PageName", $item.File.Name)
                             $webPartNodeNode.SetAttribute("FileURL", $fileUrl)
                             $xmlDoc.Save($WebParts_XML_Path)
                              
                         }
                     }                
                 }
                 else 
                 {
                     Write-Host "    Site Pages library not found." -ForegroundColor Red                
                 }            
             }
         }
     }    
     # write the output on XML File
     $xmlDoc.Save($WebParts_XML_Path)
     
     Write-Host "Completed GetWebParts Method" -ForegroundColor Yellow 
     Add-Content "$ProgressFile" "Completed GetWebParts Method"
     $script:Progress = "4:Success"
 }
 
  
 ########### End of Method #################
 
 
 

The snippet is self-explanatory. I know it is a lengthy snippet. But, wanted to share with the community.

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

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation