microsoft-graph-apimicrosoft-graph-sites

Add term to listItem in Microsoft Graph API


How do I add a term to a listItem in Microsoft Graph API?

For simple String types (ProductSegment in the example) I do the following:

PATCH https://graph.microsoft.com/v1.0/sites/{{sharepoint_site_id}}/lists/{{sharepoint_list_id}}/items/{{num}}/fields
{
    "DisplayedName": "asdasfsvsvdvsdbvdfb",
    "DocumentType": "FLYER",
    "ProductSegment": ["SEG1"],
    "TEST_x0020_2_x0020_ProductSegment": [{
        "TermGuid": "c252c37d-1fa3-4860-8d3e-ff2cdde1f673"
    }],
    "Active": true,
    "ProductSegment@odata.type": "Collection(Edm.String)",
    "TEST_x0020_2_x0020_ProductSegment@odata.type": "Collection(Edm.Term)"
}

Obviously it won't work for TEST_x0020_2_x0020_ProductSegment. But I just cannot find any hints in the documentation.


I got one step closer thanks to the duplicated issue. First I found the name (not the id) of the hidden field TEST 2 ProductSegment_0 (notice the _0 suffix). Then assembled the term value to send: -1;#MyLabel|c352c37d-1fa3-4860-8d3e-ff2cdde1f673.

PATCH https://graph.microsoft.com/v1.0/sites/{{sharepoint_site_id}}/lists/{{sharepoint_list_id}}/items/{{num}}/fields
{
    "DisplayedName": "asdasfsvsvdvsdbvdfb",
    "DocumentType": "FLYER",
    "ProductSegment": ["SEG1"],
    "i9da5ea20ec548bfb2097f0aefe49df8": "-1;#MyLabel|c352c37d-1fa3-4860-8d3e-ff2cdde1f673",
    "Active": true,
    "ProductSegment@odata.type": "Collection(Edm.String)"
}

and so I can add one item. I would need to add multiple, so I wanted to add the values to an array and set the field type (i9da5ea20ec548bfb2097f0aefe49df8@odata.type) to Collection(Edm.String).

Now I get an error with the code generalException as opposed to an invalidRequest.


Solution

  • Finally I got it.

    Thanks @Nikolay for the linked issue.

    As I also added this to the end of the question, first you need the name (not the id!) of the hidden field TEST 2 ProductSegment_0 (notice the _0 suffix). Then assemble the term values to send: -1;#MyLabel|c352c37d-1fa3-4860-8d3e-ff2cdde1f673 and -1;#SecondLabel|1ef2af46-1fa3-4860-8d3e-ff2cdde1f673, and separate them with ;# (actually the content of the label is irrelevant but some string needs to be there).

    Looks utterly ridiculous but works.

    PATCH https://graph.microsoft.com/v1.0/sites/{{sharepoint_site_id}}/lists/{{sharepoint_list_id}}/items/{{num}}/fields
    {
        "DisplayedName": "asdasfsvsvdvsdbvdfb",
        "DocumentType": "FLYER",
        "ProductSegment": ["SEG1"],
        "i9da5ea20ec548bfb2097f0aefe49df8": "-1;#MyLabel|c352c37d-1fa3-4860-8d3e-ff2cdde1f673";#-1;#SecondLabel|1ef2af46-1fa3-4860-8d3e-ff2cdde1f673,
        "Active": true,
        "ProductSegment@odata.type": "Collection(Edm.String)"
    }