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.