{"id":564,"date":"2011-11-03T17:28:39","date_gmt":"2011-11-03T22:28:39","guid":{"rendered":"http:\/\/osric.com\/chris\/accidental-developer\/?p=564"},"modified":"2013-02-19T09:15:32","modified_gmt":"2013-02-19T14:15:32","slug":"updating-a-sharepoint-pages-page-layout-using-powershell","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2011\/11\/updating-a-sharepoint-pages-page-layout-using-powershell\/","title":{"rendered":"Updating a SharePoint page&#8217;s Page Layout using PowerShell"},"content":{"rendered":"<p>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&#8217;s correlation ID in the ULS logs and found this:<br \/>\n<code>GetFileFromUrl: ArgumentException when attempting get file Url https:\/\/oldsitename\/webname\/_catalogs\/masterpage\/Block.aspx Value does not fall within the expected range.<\/code><\/p>\n<p>I couldn&#8217;t change the page layout via the browser. I couldn&#8217;t change the page layout via SharePoint Designer.<\/p>\n<p>Fortunately, I found Patrick Lamber&#8217;s helpful post, <a href=\"http:\/\/www.lamber.info\/post\/2010\/04\/02\/How-to-programmatically-change-the-page-template-in-Moss-2007.aspx\">How to programmatically change the page template in Moss 2007<\/a>, which basically described the same error and described applying a new page layout via the object model.<\/p>\n<p>For whatever reason, I thought I could do it just as easily via PowerShell. I started following Jason Grimme&#8217;s post, <a href=\"http:\/\/studioshorts.com\/blog\/2011\/01\/update-sharepoint-list-items-with-powershell\/\">Update SharePoint List Item(s) with Powershell<\/a>, but I was unable to check-out\/check-in the page or access the <em>PublishingPageLayout<\/em> property.<\/p>\n<p>Liam Cleary&#8217;s reply on a TechNet post (<a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en-US\/sharepoint2010programming\/thread\/4a9b3a4a-cafb-4e2b-8d4f-a32720ed6c3c\/\">Using PowerShell to Checkout and Checkin a file<\/a>) 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:<\/p>\n<p><code>$spWeb = Get-SPWeb(\"http:\/\/currentsitename\/webname\")<br \/>\n$spFile = $spWeb.GetFile(\"http:\/\/currentsitename\/webname\/Pages\/Test-Page.aspx\")<br \/>\n$spFile.CheckOut(\"Online\",$null)<br \/>\n$spFile.Properties[\"PublishingPageLayout\"] = \"\/_catalogs\/masterpage\/Block.aspx, Block\"<br \/>\n$spFile.Update()<br \/>\n$spFile.CheckIn(\"Update page layout via PowerShell\",[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)<br \/>\n$spWeb.Dispose()<\/code><\/p>\n<p>I visited the page and&#8230;another error! This one telling me there wasn&#8217;t a valid page layout. Funny, it worked in development.<\/p>\n<p>It turns out, when I compared it to the <em>PublishingPageLayout<\/em> property of a working page, I had missed the space character after the comma.<\/p>\n<p><strong>Doesn&#8217;t work:<\/strong><br \/>\n<code>\"\/_catalogs\/masterpage\/Block.aspx,Block\"<\/code><\/p>\n<p><strong>Works:<\/strong><br \/>\n<code>\"\/_catalogs\/masterpage\/Block.aspx, Block\"<\/code><\/p>\n<p>Once I applied the proper path, I was also able to change the page layout via the browser again.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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&#8217;t change the &hellip; <a href=\"https:\/\/osric.com\/chris\/accidental-developer\/2011\/11\/updating-a-sharepoint-pages-page-layout-using-powershell\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Updating a SharePoint page&#8217;s Page Layout using PowerShell<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[180],"tags":[228,160,211],"class_list":["post-564","post","type-post","status-publish","format-standard","hentry","category-sharepoint-2","tag-powershell","tag-sharepoint","tag-sharepoint-2010"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/564","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=564"}],"version-history":[{"count":7,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/564\/revisions"}],"predecessor-version":[{"id":918,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/564\/revisions\/918"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}