How to create site content type with id using REST API

℡╲_俬逩灬. 提交于 2020-06-17 02:17:05

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/55529315/how-to-create-site-content-type-with-id-using-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!