1

I want to create new Content Type which will be child of existing Content Type - Workflow Task (SharePoint 2013) using REST API. So when I create request, I include parent Content Type Id in new Id.

I have tried following code.

const api = '/_api/web/contenttypes';

const requestBody = {
        '__metadata':   {
            'type': 'SP.ContentType',
        },
        'Description': 'This is content type',
        'Name': 'TestContentType',
        'Group': 'TestContentTypeGroup',
        'Id': {
               '__metadata':   {
                'type': 'SP.ContentTypeId'                            
                },          
            'StringValue': '0x0108003365C4474CAE8C42BCE396314E88E51F000x010056401AE39A088645AD0597364A428033'             
        }
    };

const requestHeaders = {
    'accept': 'application/json;odata=verbose',
    'content-type': 'application/json;odata=verbose',
    'X-RequestDigest': <digestValue>
};

const requestData = JSON.stringify(requestBody);

var executor = new SP.RequestExecutor(<AppWebUrl>);
executor..executeAsync({
        url: '<BaseUrl>' + api,
        method: 'POST',
        body: requestData,
        headers: requestHeaders,
        success: res => {
            console.log(res);                 
        },
        error: error => {
            console.log(error);
        }
    });

It does create new content type TestContentType but it inherits from Item Content Type and it does't have same Id which I provided in request. It randomly generates any id.

Can anyone please help with that?

Shirin
  • 168
  • 1
  • 6

2 Answers2

2

This is actually a bug in the REST API...

Here is a link to an issue ,filed for the PnP JS library, where adding a content type is implemented the same way as you did: https://github.com/pnp/pnpjs/issues/457

Patrick Rodgers also filed an issue with Microsoft to resolve it: https://github.com/SharePoint/sp-dev-docs/issues/3276

This means that for now, unfortunately, there is no way of doing this with REST. What you can do is upvote the issue to give it more visibility and hope it will be resolved soon.

0

As far I know this bug still persist. I found workaround which can help somebody who cannot use CSOM or JSOM. You can use Site script to define new site content type and specify parent content type name or Id. This site script you can wrap in a Site design and apply this Site design through REST API. Unfortunately this workaround is useful only if you create the same site content type on different sites. And of course you can create multiple Site scripts and Site designs for each content type.