Updating a SharePoint page’s Page Layout using PowerShell

Earlier today I tried to update the page layout on a SharePoint 2010 site via the browser, which produced an error. I looked up the error’s correlation ID in the ULS logs and found this:
GetFileFromUrl: ArgumentException when attempting get file Url https://oldsitename/webname/_catalogs/masterpage/Block.aspx Value does not fall within the expected range.

I couldn’t change the page layout via the browser. I couldn’t change the page layout via SharePoint Designer.

Fortunately, I found Patrick Lamber’s helpful post, How to programmatically change the page template in Moss 2007, which basically described the same error and described applying a new page layout via the object model.

For whatever reason, I thought I could do it just as easily via PowerShell. I started following Jason Grimme’s post, Update SharePoint List Item(s) with Powershell, but I was unable to check-out/check-in the page or access the PublishingPageLayout property.

Liam Cleary’s reply on a TechNet post (Using PowerShell to Checkout and Checkin a file) tipped me off that I needed to use SPFile (rather than SPListItem) in order to perform the necessary operations. Here are the commands I ended up using:

$spWeb = Get-SPWeb("http://currentsitename/webname")
$spFile = $spWeb.GetFile("http://currentsitename/webname/Pages/Test-Page.aspx")
$spFile.Properties["PublishingPageLayout"] = "/_catalogs/masterpage/Block.aspx, Block"
$spFile.CheckIn("Update page layout via PowerShell",[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)

I visited the page and…another error! This one telling me there wasn’t a valid page layout. Funny, it worked in development.

It turns out, when I compared it to the PublishingPageLayout property of a working page, I had missed the space character after the comma.

Doesn’t work:

"/_catalogs/masterpage/Block.aspx, Block"

Once I applied the proper path, I was also able to change the page layout via the browser again.

7 thoughts on “Updating a SharePoint page’s Page Layout using PowerShell”

  1. Thanks for this! I was having the same issue with use ListItem. I was able to check the file out, but would get an error trying to check the file in. The error would say I was trying to pass a null-valued expression in as the comment string, although that wasn’t the case. After altering my code to follow this method, I was able to check out/in files without issue!

  2. FYI

    $spFile.Properties[“PublishingPagelayout”] should be “$spFile.Properties[“PublishingPageLayout”]

    The capitalization matters.

    Thanks for the code!

  3. Thanks a bunch, saved me a lot of work when after using export and import-spweb I was stuck referencing the dev site publishingpagelayout

  4. Thanks for this post man….I spent the morning fighting with this and your solution worked perfectly.

Leave a Reply

Your email address will not be published. Required fields are marked *