I wanted to use JavaScript to create a Site Page on my Sharepoint Site. I am using the following code to try to achieve that, with no success.
var fields = {
"__metadata": { 'type': "SP.Data.PagesItem"},
"Title": "Test",
}
jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Pages')/items",
method: "POST",
contentType: "application/json;odata=verbose",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
"content-Type": "application/json;odata=verbose"
},
data: JSON.stringify(fields),
success: function(data) {
console.log("OK");
},
error: function(error) {
console.log("ERRO");
console.log(JSON.stringify(error));
}
});
The problem here is, when i use this to create an item on a List, it works perfectly, but it will give me the following error when i use to create a Site Page
{"readyState":4,"responseText":"{\"error\":{\"code\":\"-2146232832, Microsoft.SharePoint.SPException\",\"message\":{\"lang\":\"en-US\",\"value\":\"To add an item to a document library, use SPFileCollection.Add()\"}}}","responseJSON":{"error":{"code":"-2146232832, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"To add an item to a document library, use SPFileCollection.Add()"}}},"status":500,"statusText":"error"}
Edit: I forgot to mention that i am using a different content type and page layout to create this page.
How can I fix this? Thank you