Search code examples
sharepointmicrosoft-graph-api

How can I create a Sharepoint List Item with related Terms/Taxononmy


The problem is that is not clear, in the docs, how to set these values in the payload. Is clear that from the UI you obviously can, but using the Graph API doesn't seem to work.

I'm trying to create a List Item, but is not clear to me how to provide the TermGuid.

I've tried something like this:

payload = {
    "fields": {
         "Title": "My item",
         "CustomMetadataField": {
              "TermGuid": "[uuid]"
         }
    }

}

But I always get a 400 error.

I also tried something like this:

payload = {"fields": {"CustomMetaDataField": "-1;#Label|uuid"}}

None of that works.


Solution

  • You can't directly set the value of your managed metadata field, but you need to set the value of the hidden column name associated with the managed metadata field.

    List all columns included the hidden columns

    GET /v1.0/sites/{site-id}/lists/{list-id}/columns?$select=id,name,hidden,displayName
    

    The response looks like this

    {
      "value": [
        {
          "displayName": "ManagedMetadata",
          "hidden": false,
          "id": "062e47e1-021e-4b11-9632-61c8968209a7",
          "name": "ManagedMetadata"
        },
        {
          "displayName": "ManagedMetadata_0",
          "hidden": true,
          "id": "08be9a90-5e7a-404e-b5f8-77121d7973c3",
          "name": "g62e47e1021e4b11963261c8968209a7"
        }
      ]
    }
    

    Suppose that the name of the field in UI is ManagedMetadata. This field has a hidden field with the displayName ManagedMetadata_0 and the name g62e47e1021e4b11963261c8968209a7 to be used in a POST request.

    POST /v1.0/sites/{site-id}/lists/{list-id}/items
    {
        "fields": {
            "{hidden_field_name}": "{TermGuid}"
        }
    }
    

    Where {hidden_field_name} in this case is g62e47e1021e4b11963261c8968209a7. The value is either {TermGuid} or -1;{TermName}|{TermGuid}.