Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
Is there an endpoint to get a wiki page with just the wiki content, without the extra html around the page? –  kimon Jul 5 '13 at 21:32
    
@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). –  Heinrich Ulbricht Jul 7 '13 at 17:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.