dimanche 1 mars 2015

SharePoint 2013: JavaScript Object Model - Create an enterprise wiki page



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.








0 commentaires:

Enregistrer un commentaire