April 6, 2012

Powershell: Get all web parts on a publishing page

There are lots of scripts available on web already for this. You must be wondering why am I posting this one?


When I tried various scripts on net to find web part on page, it gave me all content editor web parts on publishing page as error web part type.


After searching so much on internet I come through this link​ through which I was able to inspect correct type of web part. Here is the script:


[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[System.Reflection.Assembly]::LoadWithPartialName("System.Xml")
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Taxonomy")


$site = new-object Microsoft.SharePoint.SPSite "http://br41:19685/products/a-z-product-list"
$web = $site.OpenWeb()
$pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$pages = $pWeb.PagesList
$file=$web.GetFile("Pages/sas-pm.aspx")
if($file.Exists)
{
$manager = $file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
$wps = $manager.webparts
$wps | select-object @{Expression={$pWeb.Url};Label=”Web URL”},@{Expression={$fileUrl};Label=”Page URL”}, DisplayTitle, IsVisible, @{Expression={$_.RepresentedWebPartType.Name};Label=”Type”}
}
else
{
Write-Host "page not found"
}


Happy powershell scripting

If you have any questions you can reach out our SharePoint Consulting team here.

1 comment: