Friday, 14 October 2011

Powershell Script to list out webparts used in publishing site pages in Sharepoint 2007 and Sharepoint 2010

Hi,

This powershell script will enlist all webparts used in all publishing pages in a site.
This script does not take any argument at the time of invoking it. This requires an input value for the site URL. This will be asked during the execution time and a message will be flashed to provide the same.

I have included the help text to explain the purpose of this script and it can be reached by adding -help as a argument while invoking this script. This script is tested in both MOSS 2007 and SPS 2010.

param([switch]$help)

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")

function GetHelp() {


$HelpText = @"

DESCRIPTION:
This script will list out the webparts type along with the names in publishing pages in any site .

"@
$HelpText

}

function RahulPublishingPageWebParts() {

write-host "This tool will enlist the webparts used in all publishing pages in this site"
        write-host "Please enter theURL of the site"
        $siteURL = read-host
        $site = New-Object Microsoft.SharePoint.SPSite($siteURL)
        $web = $site.OpenWeb()
        $webPublish =  [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
        $pages = $webPublish.GetPublishingPages()
        foreach($page in $pages)
        {
          $manager = $web.GetLimitedWebPartManager($page.Url,[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
          $webCollection = $manager.WebParts
          if($webCollection.Count -ne 0)
{
write-host "The page "  $page.Title  " contains these webparts"
for($i =0;$i -lt $webCollection.Count; $i++)
{
write-host ($i + 1).ToString()  " "  $webCollection[$i].GetType().Name  " "  $webCollection[$i].Title
}

}
         
        }

$site.Dispose()
$web.Dispose()

       
}

if($help) { GetHelp; Continue }
else { RahulPublishingPageWebParts }

The sample output will be in this form:

I hope this will help you out.
Thanks,
Rahul Rashu

No comments:

Post a Comment