5

I have spent two days trying to create a page with a specific Content Type in Pages list using REST API. It goes into error handler and returns "{"readyState":0,"responseText":"","status":0,"statusText":"error"}"

The content type contains several custom site columns, one of them is Taxonomy Field (myCategory). when I run following query in the browser I can see the custom properties as well.

 http://publishsite/subsite1/_api/lists/getbytitle('Pages')/items

However when I run this JavaScript code to create a page it give me above error.

var url = _spPageContextInfo.webAbsoluteUrl + "_api/web/lists/GetByTitle('Pages')/items";

  var requestHeaders = {
        "accept": "application/json;odata=verbose",
        "content-type":"application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    }


   var pageData = {
        "__metadata": { "type": "SP.Data.PagesItem" },
        "Title": "title1",
        "MyNewsText": "this is test news",
        "MyNewsPuff": true,
        "MyCategory": "cat1|559d4cf3-d230-4052-ab64-09211656e8f3",
        "ContentTypeId": "0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900F1B3E07D9EBC45268BEDFAEBE1AAF9D50076E9E8FCF8CDAC46817958CF4288E0BF"
    };

var requestBody = JSON.stringify(pageData);

    $.ajax({
        url: url,
        method: "POST",
        body: requestBody,
        headers: requestHeaders,
        success: function (data) {
            alert("created");
        },
        error: function (xhr, textStatus, errorThrown) {
            alert("failed=" + JSON.stringify(xhr));
        }
    });
2

James your answer got me to look at it differently. If you want to create pages with the rest service I recommend you do the following. Create a page in the SharePoint site. Then download the page from the SharePoint server. This is the XML in the aspx page that needs to be uploaded to the server using the rest. (http://<yourSite>/pubsite/_api/Web/GetFolderByServerRelativeUrl('Pages')/Files/add(url='YourPage.aspx',overwrite=true)).

I've added only minimal amount of data to the XML.aspx page document. You can do a Rest update to any of the fields once the doc is created. First to Find the Doc you just uploaded use: http://<yourSite>/pubsite/_api/Web/GetFileByServerRelativeUrl('/pubsite/Pages/YourPage.aspx.aspx')/ListItemAllFields.
This will give you the Etag and ID number of the uploaded Document.

Then use the REST API: http://<yoursite>/pubsite/_api/Web/Lists/GetByTitle('Pages')/items(ID here). You will get some sample Json like bellow:

{
   "__metadata":{ "type":"SP.Data.PagesItem" },
   "Comments":"A test 123456"
}
1

The issue with publishing pages is that they're not just list items, the Pages library is a document library. Each page consists of a template redirection file. You've nearly got all that you need but you need to create a copy of the template direction file.

Now if you were using server side code, this is handled behind the scenes with the PublishingPage object, however in client side code you will need to automatically create this file in the Pages library. The server side code will also bind the content type of the page layout to the library also, if it doesn't already exist.

The template redirection file simply consists of the following code:

<%@ Page Inherits="Microsoft.SharePoint.Publishing.TemplateRedirectionPage,Microsoft.SharePoint.Publishing,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %> 
<%@ Reference VirtualPath="~TemplatePageUrl" %> 
<%@ Reference VirtualPath="~masterurl/custom.master" %>

And SharePoint picks this up, uses the content type to work out what page layout to use, and then the field controls on the page layout pull in the fields from the content type to render the page.

So to do this from client side code? A pain in the ass, yes. The Publishing Framework is and has always been one of the weaker components in SharePoint (for developing against, at least).

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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