0

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.

Zehui Yao
  • 87
  • 4
Balázs Németh
  • 6,222
  • 9
  • 45
  • 60

2 Answers2

0

As far as I know, graph API does not support updating SharePoint taxonomy. For now, you can go with classic SharePoint REST API for example to accomplish "advanced" things like updating taxonomy terms. Probably a duplicate of: Can't Update Sharepoint Managed Meta Data Field from Microsoft Graph Explorer

Nikolay
  • 10,752
  • 2
  • 23
  • 51
  • Thanks. This is actually helpful. I managed to add already one term to an item. Now working on being able to add a list of terms. Anyhow I'll post my findings. – Balázs Németh Oct 21 '22 at 09:45
0

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)"
}
Balázs Németh
  • 6,222
  • 9
  • 45
  • 60