Monday, November 30, 2015

Update the PageLayout using PowerShell

Update the PageLayout using PowerShell


[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")

$web = Get-SPWeb -Identity "http://myDomain.com/authoring/en-us";  #Change web that you're modifying on this line

$spPubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web);
$pages = $spPubWeb.PagesList;

foreach($item in $pages.Items)
{
  $pubPage = [Microsoft.SharePoint.Publishing.PublishingPage]::GetPublishingPage($item)

  $url = new-object Microsoft.SharePoint.SPFieldUrlValue($pubPage.ListItem[[Microsoft.SharePoint.Publishing.FieldId]::PageLayout].ToString())

  if($url -ne $null)
  {   
    if($url.Url -match 'http://mydomain.com/prod-cat/_catalogs/masterpage/TestPageLayout.aspx')  #Change Page layout name on this line
    {  
      $pubPage.Name
      $pubPage.CheckOut()    
      $pubPage.ListItem[[Microsoft.SharePoint.Publishing.FieldId]::PageLayout] = "http://mydomain.com/_catalogs/masterpage/newPageLayout.aspx"
      $pubPage.ListItem.UpdateOverwriteVersion()
      $pubPage.ListItem.File.CheckIn("Fixed URL to page layout.", [Microsoft.SharePoint.SPCheckinType]::MajorCheckIn);
    }
  }
}

No comments:

Post a Comment