5

I'm fairly new to developing SharePoint 2013 Apps, and I was tasked to customize the Create New Wiki Page for a Site. The user has to type in the Title, I do some processing, and then when everything is OK I create a new Wiki Page entry with the specified title.

What I have is the following code, which returns a Bad Request error:

executor.executeAsync(
    {
        url: appweburl + "/_api/web/GetFolderByServerRelativeUrl('/sites/doki/Wicky')/Files" +
               "/AddTemplateFile(urlOfFile='" + filename + "',templateFileType=1)",
           method: "POST",
           headers: {
               "accept": "application/json;odata=verbose",
               "content-type": "application/json;odata=verbose",
               "X-RequestDigest" : $("#__REQUESTDIGEST").val()
           },
           success: function (data) { alert('OK'); },
           error: function (data, errorCode, errorMessage) {
               alert('LOL FAIL ' + errorMessage);
           }
    }
    );

I have the basic List CRUD using JavaScript and CSOM but it does not work on a Wiki Library. How can I create a new Wiki page entry with a specified title using JavaScript and either CSOM or REST? (whichever works better)

  • 1
    Does your URL of the file include the full url to its target location? ie /wiki/page.aspx? – Hugh Wood Dec 2 '13 at 10:35
5

You did not mention what kind of error occured, but most probably it is related with filename parameter. AddTemplateFile endpoint accepts relative file Url for urlOfFile parameter.

How to create a Wiki page via SharePoint REST API

The following example demonstrates how to create Wiki page via SharePoint REST API. Since AddTemplateFile endpoint accepts relative file Url for urlOfFile parameter, the helper function getListUrl is used to resolve List Url by its title

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


//Usage
createWikiPage(_spPageContextInfo.webAbsoluteUrl,'Pages','WikiTestPage.aspx',
  function(page){  
    console.log(JSON.stringify(page));
  },
  function(error){
    console.log(JSON.stringify(error));
  }
);

Note: The request executor is not utilized in this example since it is assumed that site URL is in the same domain as the app,but if needed it could be transformed using request executor.

1

Try to encode every Strings value you are sending to the WebApp using encodeURIComponent() function, as SharePoint is very sensitive on the special characters passed in URL.

Basically, this function replace special chars like ':', '/' or ' ' by it's corresponding hexadecimal value. For instance, the whitespace char will be replaced by "%20" value.

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.