Foat Sharafutdinov
Foat Sharafutdinov

Reputation: 11

How to create a new content type in SharePoint using MS Graph / REST?

I tried to add a new content type to list using MS Graph Explorer:

Request:

POST https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/contenttypes

with body:

{
  "description": "MyCustomContentType's description",
  "group": "List Content Types",
  "hidden": false,
  "id": "0x010300B8123BA6FE3D6045BF4F6DF992B6ABE7",
  "name": "MyCustomContentType",
  "parentId": "0x0103",
  "readOnly": false,
  "sealed": false
}

Response:

{
    "error": {
        "code": "itemNotFound",
        "message": "The specified site content type was not found",
        "innerError": {
            "request-id": "1ac12fed-eaf3-4d03-a3c4-b44ddacada72",
            "date": "2020-05-16T17:12:11"
        }
    }
}

Also tried this with Graph API sdk in Java code:

IGraphServiceClient graphClient = GraphServiceClient.builder()
        .authenticationProvider(authenticationProvider)
        .buildClient();

ContentType contentType = new ContentType();
contentType.name = "MyCustomContentType";
contentType.description = "MyCustomContentType's description";
contentType.group = "List Content Types";
contentType.hidden = false;
contentType.parentId = "0x0103";
contentType.id = "0x010300B8123BA6FE3D6045BF4F6DF992B6ABE7";
contentType.readOnly = false;
contentType.sealed = false;

contentType = graphClient.sites(siteId)
        .lists(listId)
        .contentTypes()
        .buildRequest()
        .post(contentType);

the result is the same...

Also I tried to add content type to list using REST API but faced with another problem: content type is created but it ignores passed id and always inherited from Item content type. Same problem described here: How to create site content type with id using REST API. It seems like a bug of REST API.

Is it possible to create content type in SharePoint using MS Graph or REST API? Maybe there are another ways to create it using Java?

Thanks!

Upvotes: 1

Views: 928

Answers (1)

Genc Hani
Genc Hani

Reputation: 105

My answer will be a little long because I will try to explain everything as clearily as possible.

First of all, the create request for content type must be sent as POST Request to the sites/{siteId}/contenttypes endpoint. The body should be an object which contains properties: Name, Description, Group and Base. The first three properties (name, description, and group) are self-explanatory, while the Base property is just a reference object of the parent Content-Type. In the Base property, you should specify the Id of the parent content type and optionally you can also specify the name Here is the link to the official MS docs about this: Create Content Type - MS

As the base item, there is a number of pre-defined options (content types) that you can use. Note you can also use your newly created Content-Types as a base type. My suggestion is that you make a GET Request call to the https://graph.microsoft.com/beta/sites/{siteId}/contentTypes endpoint and see the results and you can copy the id of the parent content type from here.

From the results you'll get, you should see that all Content-Types that you can use as parent references. In order to get a better understanding of content types, I suggest you read about content type Id-s in this link MS Docs - Content Type Ids

So to wrap it up, your request should look something like this:

POST: https://graph.microsoft.com/beta/sites/{siteId}/contentTypes

{
    "name": "Your Content Type Name",
    "description": "Description for your content type",
    "base": {
        "name": "Name of the parent content type",
        "id": "0x0120D520" //Id of the parent content type
    },
    "group": "Your Content Type Group" 
}

Upvotes: 1

Related Questions