As part of my new Office 365 site collection provisioning (via PowerShell), I’d like to be able to set the ‘default page layout’ :
BUT – within the object model for SharePoint.Client.Publishing, there are only a handful of methods for the PublishingWebClass – and so I can’t use CSOM or POWERSHELL to set the default page layout.
Click here to see on MSDN :
But – after a quick poke into the DLL for SharePoint.Publishing.DLL (using ILSPY), I could see that the functionality is just to add a property – which is EASY via CSOM…>!
And – sure enough, if you look using the REST endpoint, there it is :
https://tenant.sharepoint.com/sites/D001N/_api/web/AllProperties
It looks like the value that I need – for the Page Layout of “ArticleLeft” is :
<layout guid=”8520f570-356b-462d-8976-1e58b936c65e” url=”_catalogs/masterpage/ArticleLeft.aspx” />
Unfortunately – that GUID is different, as you move to another site collection – even for the same page layout – doh !
Another look at ILSPY shows that the GUID is the ID of the listitem :
So – the PowerShell needs to determine the specific item for the page layout I want – and then construct the little XML chunk – and update the property for __DefaultPageLayout.
There’s a REST call that shows these guys :
And – you can actually grab the specific file :
But – from a POWERSHELL command line, this is what you need – once you’ve established a context to the specific site collection :
$rootWeb = $spoCtx.web $spoCtx.Load($rootWeb) $spoCtx.ExecuteQuery(); $spoCtx.Load($rootWeb.AllProperties) $spoCtx.ExecuteQuery(); $defaultPageLayout = '_catalogs/masterpage/ArticleLeft.aspx' #get the page item - and grab the GUID of it $urlRelative = $rootWeb.ServerRelativeUrl + "/" + $defaultPageLayout $pageLayoutFile = $rootWeb.GetFileByServerRelativeUrl($urlRelative) $spoCtx.Load($pageLayoutFile) $spoCtx.ExecuteQuery(); $pageGuid = $pageLayoutFile.UniqueId; #set the xmlchunk for the property $xmlPageLayout = "<layout guid='{0}' url='{1}' />" $xmlPageLayout = $xmlPageLayout.Replace("{0}", $pageGuid); $xmlPageLayout = $xmlPageLayout.Replace("{1}", $defaultPageLayout); $rootWeb.AllProperties["__DefaultPageLayout"] = $xmlPageLayout; $rootWeb.Update() $spoCtx.ExecuteQuery();
That works nicely – phew ! 🙂