If somebody like the post and its helpful in your work then, add comments.

Monday, October 21, 2013

Create SharePoint(2013) Publishing pages using ECMASCRIPT

Well, to create paged from page layouts in the Publishing site in SP2013, I have tried the below code

First get the context for web,

             context = new SP.ClientContext(webUrl);  web = context .get_web();

Then get the publishing web for the site

                               pubweb = SP.Publishing.PublishingWeb.getPublishingWeb(context1, web );

                                context .load(web );

                                context .load(pubweb);                               

                                 context .executeQueryAsync(onquerysucced,onqueryfailed);

In onquerysucced get all the pages in the master page library

            

var pageLibrary = web.get_lists().getByTitle("Master Page Gallery");  

var camlQuery = new SP.CamlQuery;

items = pageLibrary.getItems('');

context .load(items,'Include(Id,Title,File)');

context .executeQueryAsync(onPagelayoutsuccess,onqueryfailed);

 

In onPagelayoutsuccess method

             var listItemEnumerator = items.getEnumerator();

             var listItemInfo = '';

             while (listItemEnumerator .moveNext()) {

            var oListItem = listItemEnumerator.get_current();

                pageItems.push({

                "Id":oListItem.get_id(),

                "Title":oListItem.get_item('Title'),

                "obj" :oListItem //Add Object in the list            

                });

             pageItems.push(oListItem);

             }

             //knockout js work for finding the required page layout item from the list

             var newPage = ko.utils.arrayFirst(pageItems(), function(item) {

                                return item.Title == "CT_PageLayout.aspx";

                                }); 

            

          pageInfo = new SP.Publishing.PublishingPageInformation();

          pageInfo.set_name("CT_PageLayout.aspx");

          pageInfo.set_pageLayoutListItem(newPage.obj);

          newPage = pubweb.addPublishingPage(pageInfo);

          context.load(newPage);

          context.executeQueryAsync(onPageCreationSuccess,onqueryfailed);

         };

         function onPageCreationSuccess(){                                

                                                                listItem = newPage.get_listItem();

                context.load(listItem);

                 context.executeQueryAsync(onPageSuccess,onqueryfailed);

          };

No comments:

Post a Comment