2

I have spent a couple of days trying to fix this problem.

Goal: - Click a button on an existing page that creates an enterprise wiki page in the pages library and adopts the default custom layout.

Problem: - I am able to create the page and have it show up in my pages library, but the page doesn't take on the default layout and is also not populating the page content. Below is my code that I have used from another post.

<input id="createPageBtn" type="button" value="Create Page">
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script language="javascript">
    function createWikiPage(webUrl,listTitle,fileName,success, failure)
    {
        getListUrl(webUrl,listTitle,
          function(listUrl){

              var fileUrl = listUrl + '/' + fileName
              var url = webUrl + "/_api/web/GetFolderByServerRelativeUrl('" + listUrl + "')/Files" +
                        "/AddTemplateFile(urlOfFile='" + fileUrl + "',templateFileType=1)";
              $.ajax({
                  url: url,
                  method: "POST",
                  headers: {
                      "accept": "application/json;odata=verbose",
                      "content-type": "application/json;odata=verbose",
                      "X-RequestDigest" : $("#__REQUESTDIGEST").val()
                  },
                  success: function (data) {
                      success(data.d);

                  },
                  error: function (data) {
                      failure(data);
                  }
              });

          },
          failure
        );


    }


    function getListUrl(webUrl,listTitle,success, failure)
    {
        var url = webUrl + "/_api/web/lists/GetByTitle('" + listTitle +  "')/RootFolder";
        $.ajax({
            url: url,
            method: "GET",
            headers: {
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose"
            },
            success: function (data) {
                success(data.d.ServerRelativeUrl);
            },
            error: function (data) {
                failure(data);
            }
        });
    }

    $(document).ready(function () {
        alert("Hello World");


        $("#createPageBtn").click(function () {

            createWikiPage(_spPageContextInfo.webAbsoluteUrl, 'Pages', 'WikiTestPage.aspx',
              function (page) {

                  alert(JSON.stringify(page));
              },
              function (error) {
                  alert(JSON.stringify(error));
              }
            );
        });
    });

My speculation is that this is creating a wiki page and not an enterprise wiki publishing page. I have seen other examples online, but I can't seem to get it working with javascript. If anyone can please help or at least point me in the right direction that would be greatly appreciated.

4

Your speculation is correct, the example provided in the question is intended for creating a wiki page and not enterprise wiki publishing page.

Since you mentioned in your question JavaScript Object Model (JSOM), the below example demonstrates how to create enterprise wiki page via JSOM in SharePoint 2013.

How to create enterprise wiki page using SharePoint JSOM

function createEntWikiPage(pageName,pageContent,success,error)
{
    var templateRedirectionPageMarkup = "<%@ Page Inherits=\"Microsoft.SharePoint.Publishing.TemplateRedirectionPage,Microsoft.SharePoint.Publishing,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c\" %> <%@ Reference VirtualPath=\"~TemplatePageUrl\" %> <%@ Reference VirtualPath=\"~masterurl/custom.master\" %>";
    var ctx = SP.ClientContext.get_current();
    var wikiPages = ctx.get_web().get_lists().getByTitle("Pages");
    var fileInfo = new SP.FileCreationInformation();
    fileInfo.set_url(pageName);
    var fileContent = new SP.Base64EncodedByteArray();
    for (var i = 0; i < templateRedirectionPageMarkup.length; i++) {

        fileContent.append(templateRedirectionPageMarkup.charCodeAt(i));
    }
    fileInfo.set_content(fileContent);
    fileInfo.set_overwrite(true);

    var wikiFile = wikiPages.get_rootFolder().get_files().add(fileInfo);
    var wikiPage = wikiFile.get_listItemAllFields();
    wikiPage.set_item("PublishingPageContent",pageContent);
    var siteUrl = _spPageContextInfo.siteServerRelativeUrl == '/' ? _spPageContextInfo.siteServerRelativeUrl : _spPageContextInfo.siteServerRelativeUrl + '/';
    wikiPage.set_item("PublishingPageLayout",siteUrl + "_catalogs/masterpage/EnterpriseWiki.aspx, Basic Page");
    wikiPage.update();
    ctx.executeQueryAsync(
      function() {
         success(wikiFile);
      },
      error);
}

Usage

createEntWikiPage(
   'Greetings.aspx',
   '<h2>Welcome to SharePoint!</h2>',
   function(file){
      console.log(String.format('Enterprise wiki page {0} has been created',file.get_name()));
   },
   function(sender,args){
      console.log(args.get_message());
   });
  • 1
    Vadim, this works great. I am new to JSOM, REST Api and SharePoint development in general. You've been of great help. Could it have been possible to use the addPublishingPage() method in this situation? – Jshoe523 Mar 1 '15 at 22:20
  • Glad this helped you! Regarding addPublishingPage function from SP.Publishing namespace, this is a great idea and i think it should be possible to create ent wiki page. But in case of any difficulties feel free to contact me ;) – Vadim Gremyachev Mar 1 '15 at 22:26
  • I would like to make a quick comment that you absolutely have to use charCodeAt(). In the for loop that assigns the characters, I accidentally used charAt(), which put a bunch of encoded gibberish into the .aspx file: '&#x8;&#x1;&#x6;&#x7;&#x1; &#x1;&#x1;&#x1; &#x4;&#x2;' Pulled my hair out for a few hours before realizing my mistake. – Jim Barntish Dec 27 '16 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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