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?

Medes
  • 2,086
  • 10
  • 60
  • 103

1 Answers1

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);
Vadim Gremyachev
  • 42,498
  • 3
  • 86
  • 167
  • I see this solution works partially. But the web parts (Pre-defined web parts) in new page layout are not provisioned. Any thoughts? – Rare Solutions Feb 20 '17 at 07:10