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-$LogTimeWebParts.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.
Leave a comment