1

I need to create a Sharepoint wiki page using JSOM, Can anyone guide me to create path? I'm very very new to Sharepoint and eager to learn.

I found this but it uses Rest API and didn't find it very useful to me Creating a SharePoint Wiki Page via JavaScript and REST or CSOM

2 Answers 2

1

How to create enterprise wiki page using SharePoint 2013 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());
   });
0

The recommended approach on MSDN seems to be using the REST API. Nonetheless, the SPFolder.Files.addTemplateFile method seems to be available through CSOM/JSOM, for which it seems possible to use that approach as well.

This MSDN reference shows the available method

This reference shows the approach on CSOM (managed code). Adapting that to use JSOM looks fairly possible.

And finally this MSDN reference explains the REST approach for using addTemplateFile

Hopefully a combination of those 3 well explained posts will put you on the right track to achieve what's required.

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.