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
- Get the Web (SPWeb) instance
- Get Publishing web instance
- Get the collection of currently available page layouts
- Get Publishing site instance
- Get all page layouts collection from publishing site
- Loop through all the page layouts check for the page layout which we need to make available
- If page layout found then add it to the collection of current site page layouts
- Update the publishing web instance
Complete Script function SetAvailablePageLayout($webUrl, $pageLayoutName)
- {
- #web instance
- $web = Get-SPweb $webURL
-
- #Log file path
- $logfile="C://logupdate.txt"
- Start-Transcript $logfile
-
- #Publishing web instance
- $pweb =
- [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web);
-
- #getting current web available page layouts collection
- $currentLayOuts = $pWeb.GetAvailablePageLayouts()
-
- #site instance
- $site = $web.Site
- #publishing site instance
- [Microsoft.Sharepoint.Publishing.PublishingSite]$publishingSite = New-Object
- Microsoft.SharePoint.Publishing.PublishingSite($objSite)
-
- #getting collection of all the page layouts in a Site collection
- $allPageLayouts = $publishingSite.PageLayouts
-
- #looping through all the page layouts
- foreach($pageLayout in $allPageLayouts)
- {
-
- #checking for the page layout which we want to make available
- if($pageLayout.Name -eq $pagaeLayoutName)
- {
- #adding the new page layout to current webs available page layout
- collection
- $currentLayOuts+=$pageLayout;
- break;
- } #if
- } #for each
-
- #updating available page layout collection of current site
- $pweb.SetAvailablePageLayouts($currentLayOuts,$false)
- $pWeb.Update()
-
- } #function
SetAvailablePageLayout -WebURL "http://myweb.com/" -pageLayoutName "MyNewPageLayout.aspx"
Thanks
Feel free to comment /suggestions / feedback if any.