Thursday, October 30, 2014

Change Page Layout in SharePoint 2013 using PowerShell

In a public internet SharePoint web site based on publishing site template, We got a requirement to update existing page layouts and create new ones. So, the development team created new page layouts, Updated existing Page layouts with required changes. Any page going to be created with these updated page layouts will have the changes reflected. Well, How about existing pages created with the old page layouts? There is a requirement to switch page layouts for few pages.

While its possible to change page layout through SharePoint web interface, It would take hours to update page layout of all existing pages. So, I wrote this script to change page layout in SharePoint 2013 using PowerShell.

Change SharePoint page layout using PowerShell:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables for Web and Page URLs
$WebURL="http://portal.crescent.com/sites/sales/"
$PageURL="http://portal.crescent.com/sites/sales/Pages/About-us.aspx"

#Get the web and page
$Web = Get-SPWeb $WebURL
$File = $Web.GetFile($PageURL)

#change page layout sharepoint 2013 powershell 
$File.CheckOut("Online",$null)
$File.Properties["PublishingPageLayout"] = "http://portal.crescent.com/sites/sales/_catalogs/masterpage/About-us-v2.aspx, About us V2"
$File.Update()
$File.CheckIn("Page layout updated via PowerShell",[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)

$Web.Dispose() 

In an another case, During a site migration, I had to use this same method to change page layouts using PowerShell.
"This page is not using a valid page layout. To correct the problem edit page settings and select a valid page layout." 

This is because, Page layout URLs are hard-coded in publishing pages. When you move them from one site to another, We got to update it.

SharePoint 2010 change page layout using PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables for Web and Page URLs
$WebURL="http://www.crescent.com/company-profile/"
$OldPageLayoutName="About-us.aspx"
$NewPageLayoutName="About-us-V2.aspx"

#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 New Page Layout
$SitePageLayouts = $PublishingSite.GetPageLayouts($false)
$NewPageLayout = $SitePageLayouts | ? {$_.Name -eq $NewPageLayoutName}

#Get Pages Library
$PublishingPages = $PublishingWeb.GetPublishingPages()

#Iterate throgh each page
foreach ($Page in $PublishingPages)
{
    if ($Page.Layout.Name -eq $OldPageLayoutName)
    {
        $Page.CheckOut()
        $Page.Layout = $NewPageLayout
        $Page.ListItem.Update();
        $Page.CheckIn("Page layout Updated via PowerShell")
    
        #$page.ListItem.File.Publish("")
        if ($Page.ListItem.ParentList.EnableModeration)
        {
            $Page.ListItem.File.Approve("Publishing Page Layout Updated!");
        }

     write-host "Updated Page layout on: "$Page.url
    }
}
$Web.Dispose() 

The above script scans all publishing pages with an existing old page layout and changes page layout in SharePoint 2013 using PowerShell.



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

1 comment :

  1. You are my man. Thank you very much for this. SharePoint 2013 ones worked perfectly fine for me without a single error.

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...