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?


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_content(new SP.Base64EncodedByteArray());
   fileContent = "The content of my new file";

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


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

       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>

Does anyone knows how to add this?

1 Answer 1


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.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");  
        newPage = newPublishingPage.addPublishingPage(pageInfo);
         var wikipage = newPage.get_listItem();
        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());  

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

  • 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? Commented 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
    Commented Nov 27, 2017 at 4:15

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.