Saturday, September 24, 2016

Change Page Layout using PowerShell

Hi,

We had a requirement of changing page layout through PowerShell i.e.

If certain pages are using page layout pagelayout1,need to change those pagelayout2

So through PowerShell script, I am checking if the page is utilizing pagelayout1,

If it is, then I am changing to page layout2 of that page as below

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Variables for Web and Page URLs
$WebURL="siteurl"

#Get the web and page
$Web = Get-SPWeb $WebURL
#Get Publishing Site and Web
$PublishingSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($Web.Site)
$PublishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)

#Get Pages Library
$PublishingPages = $PublishingWeb.GetPublishingPages()
#Iterate through each page
foreach ($Page in $PublishingPages)
{
    $pageurl = $Page.url
    #Get the web and page
    $Web = Get-SPWeb $WebURL
    $File = $Web.GetFile($pageurl)

    if($File.Properties["PublishingPageLayout"].Contains("_catalogs/masterpage/pagelayout1.aspx"))
    {
    write-host "Got it"
    $File.CheckOut("Online",$null)
    $File.Properties["PublishingPageLayout"] = $WebURL+"_catalogs/masterpage/pagelayout2.aspx, Verified"
    $File.Update()
    $File.CheckIn("Page layout updated via PowerShell",[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
    }    
    
}
$Web.Dispose()

No comments:

Post a Comment