Setting Available Page Layout to Existing Sites using PowerShell

Introduction

In our project we had a requirement to available new page layout along with the existing available page layouts. Basically we need to update the available page layouts.

Since we need to make page layout available on existing sites, only option we have is PowerShell script.

Here I’ll share the detailed steps and script

Following are the high level steps

  1. Get the Web (SPWeb) instance
  2. Get Publishing web instance
  3. Get the collection of currently available page layouts
  4. Get Publishing site instance
  5. Get all page layouts collection from publishing site
  6. Loop through all the page layouts check for the page layout which we need to make available
  7. If page layout found then add it to the collection of current site page layouts
  8. Update the publishing web instance

Complete Script function SetAvailablePageLayout($webUrl, $pageLayoutName)  

  1. {  
  2.     #web instance  
  3.           $web = Get-SPweb $webURL  
  4.      
  5.     #Log file path  
  6.     $logfile="C://logupdate.txt"  
  7.     Start-Transcript $logfile  
  8.  
  9.     #Publishing web instance  
  10.     $pweb =   
  11.           [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web);  
  12.      
  13.          #getting current web available page layouts collection  
  14.     $currentLayOuts = $pWeb.GetAvailablePageLayouts()  
  15.      
  16.     #site instance  
  17.     $site = $web.Site  
  18.     #publishing site instance  
  19.     [Microsoft.Sharepoint.Publishing.PublishingSite]$publishingSite = New-Object    
  20.           Microsoft.SharePoint.Publishing.PublishingSite($objSite)  
  21.      
  22.          #getting collection of all the page layouts in a Site collection  
  23.     $allPageLayouts = $publishingSite.PageLayouts  
  24.  
  25.     #looping through all the page layouts  
  26.     foreach($pageLayout in $allPageLayouts)  
  27.         {  
  28.          
  29.            #checking for the page layout which we want to make available  
  30.       if($pageLayout.Name -eq $pagaeLayoutName)  
  31.       {  
  32.         #adding the new page layout to current webs available page layout            
  33.                 collection  
  34.                 $currentLayOuts+=$pageLayout;  
  35.         break;  
  36.        } #if  
  37.     } #for each  
  38.      
  39.     #updating available page layout collection of current site  
  40.     $pweb.SetAvailablePageLayouts($currentLayOuts,$false)  
  41.     $pWeb.Update()  
  42.       
  43. } #function  
SetAvailablePageLayout -WebURL "http://myweb.com/" -pageLayoutName "MyNewPageLayout.aspx"

Thanks

Feel free to comment /suggestions / feedback if any.
Ebook Download
View all
Learn
View all