0

I want to add pages on pages list, when btnAddPage is clicked opens a dialog with two inputs, addPageTitle and addPageTags but I don't know how to create those pages. I've already created Items to a custom list I've made and I've tried using the same function but it didn't work. Does anyone know how to add those two fields on a new item on pages list?

Update:

I've made some research and manage to find this code:

function createFile(resultpanel) {
   var clientContext;
   var oWebsite;
   var oList;
   var fileCreateInfo;
   var fileContent;

   clientContext = new SP.ClientContext.get_current();
   oWebsite = clientContext.get_web();
   oList = oWebsite.get_lists().getByTitle("Pages");

   fileCreateInfo = new SP.FileCreationInformation();
   fileCreateInfo.set_url("title.aspx");
   fileCreateInfo.set_content(new SP.Base64EncodedByteArray());
   fileContent = "The content of my new file";

   for (var i = 0; i < fileContent.length; i++) {

       fileCreateInfo.get_content().append(fileContent.charCodeAt(i));
   }

   this.newFile = oList.get_rootFolder().get_files().add(fileCreateInfo);

   clientContext.load(this.newFile);
   clientContext.executeQueryAsync(
       Function.createDelegate(this, successHandler),
       Function.createDelegate(this, errorHandler)
  );

function successHandler() {
    resultpanel.innerHTML =
        "Go to the " +
        "<a href='../Pages'>document library</a> " +
        "to see your new file.";
}

function errorHandler() {
    resultpanel.innerHTML = "Request failed: " + arguments[1].get_message();
}
}

That works perfectly but still can't add anything to a field Tags.

Update 2:

The answer below indeed adds a page but a blank one, the whole document(site) is blank. So I went through Rest Api check what was wrong and found that was missing some attribute:

<d:PublishingPageLayout m:type="SP.FieldUrlValue">
  <d:Description>Basic Page</d:Description>
<d:Url>http://deliveryoffice.telecom.pt/_catalogs/masterpage/EnterpriseWiki.aspx</d:Url> 
</d:PublishingPageLayout>

Does anyone knows how to add this?

1 Answer 1

1
+50

Use set_item to set the associated field data for the page item and create with a page layout in master page Gallery like below:

 <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
<script type="text/javascript">  
    $(document).ready(function() {  
        var scriptbase = _spPageContextInfo.webAbsoluteUrl+ "/_layouts/15/";  
          $.getScript(scriptbase + "SP.Runtime.js", function() {  
            $.getScript(scriptbase + "SP.js", function() {  
                $.getScript(scriptbase + "SP.Publishing.js", createPublishingPage);  
            });  
        });          
    });  
    var oWeb, clientContext, pageLayoutitem;  

    function createPublishingPage() {  
        //Get the client context,web and list object(Master Page Gallery)   
        clientContext = new SP.ClientContext.get_current();  
        oWeb = clientContext.get_web();  
        var oList = oWeb.get_lists().getByTitle('Master Page Gallery');  
        //Get the page layout by ID using which we will create a publishing page   
        pageLayoutitem = oList.getItemById(7);  
        //Load the client context and execute the batch   
        clientContext.load(oWeb);  
        clientContext.load(pageLayoutitem);  
        clientContext.executeQueryAsync(QuerySuccess, QueryFailure);  
    }  

    function QuerySuccess() {  
        //Create Publishing Page using PublishingPageInformation object   
        var newPublishingPage = SP.Publishing.PublishingWeb.getPublishingWeb(clientContext, oWeb);  
        var pageInfo = new SP.Publishing.PublishingPageInformation();  
        pageInfo.set_name("New Publishing Page.aspx");  
        pageInfo.set_pageLayoutListItem(pageLayoutitem);  
        newPage = newPublishingPage.addPublishingPage(pageInfo);
        console.log(newPage);
         var wikipage = newPage.get_listItem();
         console.log(wikipage);
           wikipage.set_item("Title","test");
           wikipage.set_item("tags","testtag");
           wikipage.update();
           clientContext.load(newPage);  
           clientContext.load(wikipage);
        clientContext.executeQueryAsync(SecondQuerySuccess, SecondQueryFailure);  
    }  

    function QueryFailure(sender, args) {  
        console.log('Request failed' + args.get_message());  
    }  

    function SecondQuerySuccess(sender, args) {  
        console.log("Publishing page created successfully.");  
    }  

    function SecondQueryFailure(sender, args) {  
        console.log('Request failed' + args.get_message());  
    }  
</script>

This is the page layout in Master Page Gallery and the id is 7 enter image description here

This is the result, it will create a publishing page based on the default layout: enter image description here

Reference Demo from here:

Create A Publishing Page In SharePoint Using Javascript Object Model

2
  • I just realized that your code doesn't add Page Layout so when I open it, it's blank. How can I add it's layout? Nov 23, 2017 at 16:52
  • Create Page item based on a page layout, need to firstly get page layout from Master Page Gallery, I have updated the code in the answer, please check
    – Jerry
    Nov 27, 2017 at 4:15

Your Answer

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

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