Question

I'm trying to create a publishing page following this thread and the page is being created in the library. Basically using creating a template

<%@ Page Inherits="Microsoft.SharePoint.Publishing.TemplateRedirectionPage,Microsoft.SharePoint.Publishing,Version=15.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %><%@ Reference VirtualPath="~TemplatePageUrl" %><%@ Reference VirtualPath="~masterurl/custom.master" %>

and then sending that as the body to this REST endpoint

/_api/Web/GetFolderByServerRelativeUrl('Pages')/Files/add(url='" + pageName + ".aspx',overwrite=true)

This creates a page, but doesn't have the correct page layout or content type.

I tried using REST to set the pagelayout and it didn't throw an error, but it also didn't work. I found this thread which shows how to set the page layout via JSOM. I chain it with my REST call to get the docId of the new page and then call this:

var ctx = SP.ClientContext.get_current();
var listPages = ctx.get_web().get_lists().getByTitle('Pages');
var pageItem = listPages.getItemById(docId);

var val = new SP.FieldUrlValue();
val.set_url('_catalogs/masterpage/PageLayouName.aspx');
val.set_description('PageLayoutName');
pageItem.set_item('PublishingPageLayout', val);
pageItem.update();

That works and does change the page layout for the page. When I open the page for editing I see the metadata associated with the contenttype for the page layout and I see the various webpart zones as defined for my page layout. The pagelayout has several zones, page fields, content query webpart, etc. It almost seems to work, except...

In my page layout I have a content editor webpart snippet defined. That webpart doesn't show on the page. The zone it is in is there but the webpart just isn't there.

If I reapply the page layout from the UI using the ribbon the missing webpart shows up.

How can I create a page that properly uses its page layout?

Was it helpful?

Solution

If your CEWP is inside a WebPart zone into your Page Layout, it will be ignored when you apply the layout to your newly created page.

You have 2 options:

  1. Edit the page layout and put your CEWP outside the WP zone. Then the CEWP will be available on all pages and couldn’t be removed afterwards on pages
  2. Provision your CEWP by CSOM/JSOM code using LimitedWebpartManager.AddWebpart (sample usage)

Hope it will help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top