Friday 23 May 2014

Powershell script to get the list of all available webparts on each page with in the sites.

# Add SharePoint cmdlets reference
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

function enumerateWebParts($Url) {
    $site = new-object Microsoft.SharePoint.SPSite $Url

    foreach($web in $site.AllWebs) {
      
        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
        try
        {              
            $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
        }
        catch [Exception]
        {
            $fileUrl = $webUrl + "/" + $item.File.Url
            write-host "error occured processing the page     $fileUrl"      
            continue;
        }  
                $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
            try
            {                   
                $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
            }
            catch [Exception]
            {
                $fileUrl = $webUrl + "/" + $item.File.Url
                write-host "error occured processing the page     $fileUrl"      
                continue;
            }
                    $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
            }
        }
    $web.Dispose()
    }

 }
$webAppUrl = Read-Host "Enter the web application URL for ex:test.com"
#$csvpath = Read-Host "Enter the csv file path to create for ex: c:\test.csv"

$row = enumerateWebParts($webAppUrl)
$row | Out-Gridview

No comments:

Post a Comment