2

I would like to create a Site Page via REST API with my own Title and Content. So far I tried to send a POST request but got an error:

To add an item to a document library, use SPFileCollection.Add()

POST: <url>/_api/web/lists/GetByTitle('Site Pages')/Items

Body:

{
  "__metadata": {
    "type": "SP.Data.SitePagesItem"
  },
  "Title": "It's working?"
}

From what I understand - first I need to create a Site Page (a file) in SharePoint and then modify it via REST. However, my requirement is to create the entire entry via REST only.

How can this be done?

1

This is a great article explaining how to do this. Essentially, it creates a new .aspx file in the Site Pages library. Here is the code they use (called from a page on the target web):

function CreateWikiPage() {

// Get Server relative url of Web(site)
var WebServerRelativeUrl = _spPageContextInfo.webServerRelativeUrl;

// Provide Internal name of the library here
var DocuentLibraryInternalName = "SitePages";

// Provide name of the wiki page to be created
var NewPageName = "NewRESTWikipage.aspx";

// Form relative url of the new page. This will be used in function below
var NewFileUrl = WebServerRelativeUrl + "/" + DocuentLibraryInternalName + "/" + NewPageName;

$.ajax({

    // "templateFileType" values in below method
    // StandardPage. The value = 0.
    // WikiPage. The value = 1.
    // FormPage. The value = 2.

    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFolderByServerRelativeUrl('" + WebServerRelativeUrl + "/" + DocuentLibraryInternalName + "')/Files/AddTemplateFile(urlOfFile='" + NewFileUrl + "',templateFileType=1)",
    method: "POST",
    headers: {
        "accept": "application/json;odata=verbose",
        "content-type": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function (data, status, xhr) {
        console.log("Success");
    },
    error: function (xhr, status, error) {
        console.log("Failed");
    }
});
}
| improve this answer | |
  • The given code creates a "Wiki Page" instead of "Site Page". From what I understand, this "Wiki Page" is an old version of SharePoint entries. I need to create a "new" version of this entry - so "Site Page" instead of "Wiki Page". Nevertheless, thanks for the Pro Tip :) – Sokołow Aug 25 at 14:51
0
  1. Create the Template of a "modern entries" in SharePoint. Follow this guide to do that.

  2. Create a new Site Page via REST API using that saved template. Just copy it from Template folder to Root folder of that subpage: /_api/Web/GetFileByServerRelativeUrl('<source_url>')/CopyTo(strnewurl='<target_url>',bOverwrite=false)

  3. Edit that newly created Site Page as you wish (eg. via REST API).

Source and Target URLs:

source_url - /sites/<your_site>/SitePages/Templates/Your-Template.aspx

target_url - /sites/<your_site>/SitePages/New-Modern-Page.aspx

| improve this answer | |

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.