Updating a SharePoint List Item’s Content Type in the Microsoft Graph

The Microsoft Graph Documentation on how to update a SharePoint list item describes only how to update the fields.

This is done by a PATCH call to:

PATCH https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items/{item-id}/fields
{
    "{Field Name}": "{Value}"
}

Which will, in turn, return a set of fields that you may or may not have changed.

2018-09-05_21-53-16

But what if you want to change the content type?

This is not a big ask, and there are plenty of situations where you want to. Especially when uploading a file via the Graph (using the Drive portion of the API) which sets the default content type and therefore requiring you to make a second call to update the content type (and set any additional field values).

You can see in the above image there is a “ContentType” value, but setting that is not as successful as setting a field value.

2018-09-05_21-56-29
Yes, that is a valid content type.

Those with previous SharePoint API knowledge would point out it should be the ContentTypeId that needs setting; and although it is not visible in the field result set above, you can reference it. But it still doesn’t help.

2018-09-05_21-59-47
Field ‘ContentTypeId’ is read-only

So how is it done? It is perfectly reasonable to want to change the content type and something that is able to be done in other SharePoint APIs, so what is wrong here?

Turns out, you need to do this on the item itself, not in a call to /fields.

PATCH https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items/{item-id}
{
    "contentType": {
        "id": "{Content Type Id}"
    }
}
2018-09-05_22-15-08
It worked!

What is returned is not the set of fields, but the properties of the list item itself, including the content type and the list of fields.

But if you are making the call to the item and not to /fields, does this mean you need to make a call to set the content type, and then another to set the field values?

Nope. You can also set the field values in the same call; just wrap them up in a fields object.

PATCH https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items/{item-id}
{
    "contentType": {
        "id": "{Content Type Id}"
    },
    "fields": {
        "{Field Name}": "{Value}"
    }
}

2018-09-05_22-21-05

The only thing I want to point out is “contentType” and “fields” need to be camel case.

Versioning issues

If you care about document versions, note that this call will uprev your item in SharePoint twice when you wish to update the fields.

If you are updating the content type only, this will not happen.

If you don’t care about updating the content type and only want to update the fields, then make the PATCH call to /fields instead.

Why did I feel it necessary to write this post?

As I pointed out above, the Microsoft Graph documentation is sorely lacking; and although all the documentation is on GitHub and open to the public to adjust, the page that I believe would be best to adjust (updating a list item) is focused around making a call to /fields and doesn’t really allow conversation on calls anywhere else.

Comments are closed.

Blog at WordPress.com.

Up ↑

%d bloggers like this: