Updating Page Layout and Content Type using PowerShell

In this blog we are going see how to change page layout and content type of publishing pages in document library using PowerShell script.

Page Layout and Content Type

Page layout is a publishing feature that defines the layout of a page. So we need to import the Microsoft.SharePoint.Publishing.dll into PowerShell. Then follow these steps:

  1. Get the object of PublishingWeb from SPWeb.

    $publishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web);
     
  2. Get the object of page layout from then title of page layout.

    $allPageLayouts = $publishingWeb.GetAvailablePageLayouts();
    $pageLayout = $null;
    foreach($pl in $allPageLayouts){
        if($pl.Title -eq $NewPageLayout){
            $pageLayout = $pl;
            break;
        }
    }
     
  3. Then get the object of publishing page object using page url and publishing web object.

    $page = $publishingWeb.GetPublishingPage($pageUrl);
     
  4. Once you get the publishing page object then we can update the page layout:

    $page.Layout = $pageLayout;
     
  5. And then update the content type associated with the page layout to the page.

    $page.ListItem[[Microsoft.SharePoint.SPBuiltInFieldId]::ContentTypeId] = $pageLayout.AssociatedContentType.Id;
    $page.ListItem[[Microsoft.SharePoint.SPBuiltInFieldId]::ContentType] = $pageLayout.AssociatedContentType.Name;
     
  6. Then CheckIn and Update the page.

    $page.CheckIn('Checkin');
    $page.Update();

PowerShell Script

Attached is the complete PowerShell script. Example

.\UpdatePageLayoutAndContentType -SiteCollectionUrl "<Site Collection Url>" -SiteTitle "<Site Title>" -OldPageLayout "Old Page Layout Title" -NewPageLayout "New Page Layout Title" -FolderPath "Document Library Folder"

Summary

In this article I’ve discussed how to PowerShell script to change and update page layout and content type.

References

  1. PublishingWeb Class
  2. PublishingPage.Layout Property 
Next Recommended Reading Create Content Type Using PowerShell