kwizcom banner advertisement

Get SharePoint Page Layouts Usage Analysis Report using PowerShell

Requirement: Get an inventory of all available Page layouts and their usage in SharePoint site Collection.

How to Get All Page Layouts in SharePoint?
If you get into Site Settings page, You can find page layouts either under "Page layouts and site templates" or "Master pages and page layouts" links.
Get SharePoint Page Layouts, Usage Analysis using PowerShell

Get All Available Page Layouts of a Publishing Site Collection
SharePoint Page layouts are scoped at site collection level. 
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function Get-PageLayouts
{
    param
    (
        [Parameter(Mandatory=$true)][Microsoft.SharePoint.SPSite]$Site
    )
    
    #Get the Publishing Site object
    [Microsoft.Sharepoint.Publishing.PublishingSite]$PublishingSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($Site)
    
    #Get All page layouts  
    $PageLayouts = $PublishingSite.GetPageLayouts($false)
        
    ForEach($PageLayout in $PageLayouts)
    {
            Write-host $PageLayout.Name : $PageLayout.ServerRelativeUrl
    }
}

#Get a Site collection
$Site = Get-SPSite "https://Opera.crescent.com/"

#Call the function to get all available page layouts
Get-PageLayouts -Site $Site

Get Page Layouts Usage in SharePoint Site Collection using PowerShell:
Now, lets audit page layouts usage in a particular site collection .
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function Get-PageLayoutsUsage
{
    param
    (
        [Parameter(Mandatory=$true)][Microsoft.SharePoint.SPSite]$Site,
        [Parameter(Mandatory=$true)][String]$ReportFile
    )
    
    #Array to store Result
    $ResultSet = @()
 
    #Iterate through each web of the site collection
    ForEach($web in $Site.AllWebs)
    {
        Write-host -f Cyan "Scanning site $($Web.URL)..."
        $PublishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
        
        if($PublishingWeb.PagesList)
        {
            foreach($Page in $PublishingWeb.GetPublishingPages())
            {
                #Get the page layout details    
                $Result = new-object PSObject
                $Result | add-member -membertype NoteProperty -name "URL" -Value $web.Url
                $Result | add-member -membertype NoteProperty -name "Page URL" -Value $Page.Uri.ToString()
                $Result | add-member -membertype NoteProperty -name "PageLayout URL" -Value $Page.Layout.ServerRelativeUrl
                $Result | add-member -membertype NoteProperty -name "PageLayout Name" -Value $Page.Layout.Name

                $ResultSet += $Result
            }            
        }
    }
    #Export Result to csv file
    $ResultSet |  Export-Csv $ReportFile -notypeinformation
    
    Write-Host "Page Layouts Usage Report Generated Successfully!" -f Green
}

#Get a Site collection
$Site = Get-SPSite "https://portal.crescent.com/"
$ReportFile="C:\Temp\PageLayouts.csv"

#Call the function to get page layouts usage
Get-PageLayoutsUsage -Site $Site -ReportFile $ReportFile

This script generates a CSV file in the below format:
sharepoint page layout usage report
Get SharePoint Page Layouts Usage Analysis Report using PowerShell Get SharePoint Page Layouts Usage Analysis Report using PowerShell Reviewed by Salaudeen Rajack on August 09, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.