1

I am trying to use SharePoint REST to create a content type. I intended the content type to use the 'Document' as its parent (0x0101). But every time I tried the code, it creates a content type with 'Item' as its parent(0x01).

Here is the complete code:

// URL variable
var url = _spPageContextInfo.webAbsoluteUrl

// Options variable
var options = {
    headers: {
      "Content-Type": "application/json;odata=verbose",
      "accept":  "application/json;odata=verbose",
      "X-RequestDigest": _spPageContextInfo.formDigestValue
    },
    credentials: 'include',
    method: 'POST',
    body: JSON.stringify({
    __metadata: {
        type: "SP.ContentType"
    },
    Id: {
        __metadata: {
            type: "SP.ContentTypeId"
        },
        StringValue: '0x01010040C0C59A4C644983AE2877D595FC9D38'
    },
    Name: 'My New Document',
    Description: 'My New Document',
    Group: 'My New Document'
        })
}

// Run fetch on the browser console.
fetch(                     
  url + "/_api/web/contenttypes",                     
  options              
).then( 
  res => res.json().then(json => console.dir(json)) 
)

Can anyone show me what is missing?

0

As mentioned in below to links:

  1. Create content type using REST
  2. Content type id ignored when using sp.web.contenttypes.add

Content types are very broken in REST API and one sould not use REST to do anything with content types.

So, I will suggest you to use the JSOM to create a content type where you can easily mention the Parent content type using set_parentContentType().

Code for reference:

<script src="https://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var contentTypeCollection;

    function createContentType() {
        var clientContext = new SP.ClientContext.get_current();
        if (clientContext != undefined && clientContext != null) {
            var web = clientContext.get_web();
            this.contentTypeCollection = web.get_contentTypes();
            this.contentType = contentTypeCollection.getById("0x0101");

            var newContentType = new SP.ContentTypeCreationInformation();
            newContentType.set_name('My Custom Content Type');
            newContentType.set_description('My custom content type');
            newContentType.set_parentContentType(contentType);
            this.contentTypeCollection.add(newContentType);

            clientContext.load(this.contentTypeCollection);
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }
    }

    function onQuerySucceeded() {
        alert("Content Type created successfully")
    }

    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
</script>

Source: SharePoint Online: How to create Content Type using JavaScript object model?

Similar Issue: How to create site content type with id using REST API.

1
  • Thank you Ganesh – Roy Selim Feb 22 '20 at 22:51

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.