0

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

0

Items in a library are associated with files, so you cannot simply create items in the Site Pages library.

As far as I know and have used it, the SPFileCollection.Add() method is useful for uploading files not creating pages, regardless if you're using managed code (C#) or REST APIs.

There is a different REST endpoint to use for creating pages: <YOUR_SITE_URL>/_api/web/getfolderbyserverrelativeurl('/SitePages')/files/addtemplatefile(urloffile='/SitePages/<YOUR_PAGE_NAME>.aspx',templatefiletype=0). Just replace the all caps words and the brackets around them to make it work.

The value of the templatefiletype property in the REST endpoint depends on what type of page you are trying to create: StandardPage = 0, WikiPage = 1, FormPage = 2, ClientSidePage = 3. A client side page is a modern page.

You can also use a JS library like PnP JS to create pages, which can take care of some of the plumbing for you. Here is how you can create wiki page and here how to create client-side pages.

References:

  • If i use the different rest endpint, it won't let me change the "fields content". It will say that "he parameter __metadata does not exist in method AddTemplateFile." Is there an workaroudn for this? – João Feb 14 '19 at 12:25
0

Assuming that you would like to create the blank page based on certain template, the REST api's for files and folders to be used are not similar to the ones used for creating list items.

To add a page with .aspx extension with certain template.

url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/getfolderbyserverrelativeurl('/SitePages')/files/addtemplatefile(urloffile='/SitePages/newwebpartpage.aspx',templatefiletype=0)",

here templatefiletype=0 creates a standard page, refer this link to create other types of pages.

To add any files to document library use below REST endpoint.

url: "_api/web/lists/getByTitle('Shared Documents')/RootFolder/Files/Add(url='a.dat', overwrite=true)",
  • If i use the first url, i get the following error: "The parameter __metadata does not exist in method AddTemplateFile." If i remove that parameter, it says that the next parameter does not existe. Is there any way i can get around this issue? – João Feb 14 '19 at 11:42
  • Hi, did you ever find any solution to ""The parameter __metadata does not exist in method AddTemplateFile." ?? – Muhammad Murad Haider Dec 19 '19 at 10:21

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.