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

Thanks

Feel free to comment /suggestions / feedback if any.