2

I've seen a lot of PS that list all web parts in site collections but is limited to the pages and site pages library. Can you modify this script to also include web parts in the root default.aspx?

function enumerateWebParts($Url) {
$webApp = Get-SPWebApplication $Url  #Get WebApplication URL

    foreach($web in $webApp.AllWebs| Get-SPSite -Limit All | Get-SPWeb -Limit All) {
        if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)) {
            $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
            $pages = $pWeb.PagesList

            Write-Host "Processing Web:" $pWeb.Url "..." -ForegroundColor Magenta

            foreach ($item in $pages.Items) {
                $fileUrl = $webUrl + "/" + $item.File.Url
                Write-Host "   " $fileUrl -ForegroundColor Green      
                $manager = $item.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={$_.GetType().ToString()};Label="Type"}                
            }            
        }
        else {
            Write-Host "   Not a publishing web:" $web.Url". Looking for Site Pages library." -ForegroundColor Magenta
            $pages = $null
            $pages = $web.Lists["Site Pages"]

            if ($pages) {
                Write-Host "   " $pages.Title "found." -ForegroundColor Green

                foreach ($item in $pages.Items) {
                    $fileUrl = $webUrl + "/" + $item.File.Url
                    $manager = $item.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={$_.GetType().ToString()};Label="Type"}
                }                
            }
            else {
                Write-Host "    Site Pages library not found." -ForegroundColor Red                
            }            
        }

        Write-Host "... completed processing" $web "..." -ForegroundColor Magenta    
    }
}
$row = enumerateWebParts('http://servername.com/')
$row | Out-GridView
4

The method GetLimitedWebPartManager can also be used with the SPWeb object, taking in parameter the url of the page.

$wpMngr = $web.GetLimitedWebPartManager("default.aspx",[System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
foreach($wp in $wpMngr.WebParts)
{ ... }

You could also directly get the page then call the GetLimitedWebPartManager

$file = $web.GetFile("default.aspx");
$wpMngr = $file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);

Ref: https://msdn.microsoft.com/EN-US/library/microsoft.sharepoint.spweb.getlimitedwebpartmanager.aspx

https://msdn.microsoft.com/EN-US/library/microsoft.sharepoint.spfile.getlimitedwebpartmanager.aspx

  • Thanks for the reply! Where do I insert the statement so that the script will run on pages, site pages, and root default.aspx? – Dixie Sep 30 '15 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.