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());
});