7

I want to change page layout for some of my publishing pages using a SharePoint-Hosted app.

Is there a way to changing (setting) page layout to a publishing page using JavaScript?

1 Answer 1

5

How to update Page Layout for a Page via CSOM (JavaScript)

function updatePublishingPage(pageUrl,properties) {

    var context = SP.ClientContext.get_current();
    var site = context.get_site();    
    var web = context.get_web();
    var pageFile = web.getFileByServerRelativeUrl(pageUrl);
    var pageItem = pageFile.get_listItemAllFields();
    context.load(site);
    context.load(pageItem);

    context.executeQueryAsync(
        function () {

            for(var propName in properties) {
               var property = properties[propName]; 
               var itemValue = pageItem.get_item(propName);
               if(property.Type == "Url") {
                   var pagelayoutUrl = site.get_url() + property.Value.split(',')[0].trim();
                   itemValue.set_url(pagelayoutUrl);
                   var pagelayoutDesc = property.Value.split(',')[1].trim();
                   itemValue.set_description(pagelayoutDesc);
                   pageItem.set_item(propName,itemValue);
               }

            }
            pageItem.update();
            context.load(pageItem);
            context.executeQueryAsync(
                function () {
                   console.log(pageItem);
                },
                function (sender, args) {
                   console.log('Failed: ' + args.get_message());
                }
            );    
        },
        function (sender, args) {
            console.log('Failed: ' + args.get_message());
        }
   );

}

Usage

 var properties = {'PublishingPageLayout' : {'Type': 'Url','Value': '/_catalogs/masterpage/ArticleLeft.aspx, Image on left'}};  //Image on Left page layout
 updatePublishingPage('/news/Pages/Latest-News.aspx',properties);
1
  • I see this solution works partially. But the web parts (Pre-defined web parts) in new page layout are not provisioned. Any thoughts? Commented Feb 20, 2017 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.