4

How do you correctly update a SharePoint 2013 wiki page using the REST API?

Three basic capabilities I am interested in:

  • Reading an existing page (content only)
  • Updating an existing page
  • Creating a new page

For reading an existing page, of course I can just to a "GET" of the correct URL, but this also brings down all the various decorations around the actual data on the wiki page-- rather than fish that out myself, it would be better if there was a way to just get the content if that is possible.

Are there special endpoints is the REST API that allow for any of these three operations on wiki pages?

1 Answer 1

4

There are some samples for uploading a generic file using the [...]/Add REST endpoint:

http://<sitecollection>/<site>/_api/web/folders/getbyurl(folderrelativeurl)/files/add(parameters)

(MSDN)

But Wiki pages need to be added using the [...]/AddTemplateFile REST endpoint:

http://<sitecollection>/<site>/_api/web/folders/getbyurl(folderrelativeurl)/files/addTemplateFile(urlOfFile,templateFileType)

(MSDN)

The code to add a new empty Wiki page to a wiki document library using REST looks like this:

$.ajax({
    url: _spPageContextInfo.webServerRelativeUrl +
    "/_api/web/GetFolderByServerRelativeUrl('" + serverRelativeUrl + "')/Files" +
    "/AddTemplateFile(urlOfFile='" + filename + "', templateFileType=1)",
    type: "POST",
    headers: {
        "accept": "application/json;odata=verbose",
        "content-type": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
    },
    success: function (data) {
        // call function do fill wiki with content
    },
    error: function (err) {
        // handle error
    }
});

The key is to use AddTemplateFile with the correct template file type for a wiki page. templateFileType is defined in SP.js:

SP.TemplateFileType = function() {
};
SP.TemplateFileType.prototype = {
    standardPage: 0,
    wikiPage: 1,
    formPage: 2
};

To update the Wiki page content you have to set the WikiField property of the item. This can be done posting to the [...]/Items REST endpoint as shown here. The key is to set the data correctly:

data: JSON.stringify({
        "__metadata": { "type": getListType(listname) },
        "WikiField": "content goes here"
    } ),

And that's it.

2
  • Is there an endpoint to get a wiki page with just the wiki content, without the extra html around the page?
    – kimon
    Commented Jul 5, 2013 at 21:32
  • 2
    @kimon I think so, never did it myself though. Try getting the WikiField content by issuing a GET on the [...]/Items endpoint (instead of POST for setting it). Commented Jul 7, 2013 at 17:42

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.