How to set a Term into the TaxKeyword field of a file

jobs 20 Reputation points
2023-07-26T08:52:58.51+00:00

I've been working with the TermStore in Microsoft Graph, using the official documentation I've managed to get a CRUD system of Terms. The whole point of the TermStore is using these terms with files, so I thought it would be also easy to add those terms into files, foolish of me to think that. I've tried to search, and I didn't find any way to do that, I know using the SharePoint REST API or CSOM or PnP is possible, but it feels to me very anti-intuitive, why Microsoft would give the possibility to perform CRUD operations on terms, but then there is no way to add those terms into files? Maybe it's just me that I didn't find how to do it.

This is what I've tried:

If I make this HTTP call, the object that I receive doesn't show the Taxonomy property, so I can't make a PATCH call to modify that value because it simply doesn't appear.

GET https://graph.microsoft.com/v1.0/sites/{site-id}/drive/items/{item-id}?expand=listItem

And if I do this HTTP call, the object has the Taxonomy property

GET https://graph.microsoft.com/v1.0/sites/{site-id}/lists/Documents/items/{item-id}?expand=fields

So I've tried to make a PATCH, sending this to the body

{
    "fields": {
        "TaxKeyword@odata.type": "#Collection(microsoft.graph.Json)",
        "TaxKeyword": [
            {
                "Label": "Test1",
                "TermGuid": "1b20b62d-7ee9-4f55-9ecf-88e622a3f7d7"
            }
        ]
    }
}

(I am using an existing Term that I've created with the CRUD system, but I don't know how to get the WssId, I am not sure if it is necessary or if it is even possible to obtain it)

But it appears the generic error "invalidRequest".

And is not a problem of permissions, my app has Files.ReadWrite.All, Sites.ReadWrite.All and TermStore.ReadWrite.All

I hope that there is a solution, and it's just me that I can't search well

Thank you

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,445 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2023-07-26T08:52:58.5133333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To add the Term into the TaxKeyword field of a file using the Graph API, you can make a PATCH request to the driveItem with the fields query parameter and specify the TaxKeyword field. Here's an example JSON body you can use:

    {
       "fields": {
          "@odata.type": "microsoft.graph.driveItem",
          "taxKeyword": [
             {
                "@odata.type": "#microsoft.graph.taxonomyFieldValue",
                "label": "Term Label",
                "termGuid": "TermGuid",
                "wssId": "WssId"
             }
          ]
       }
    }
    

    Please note that to create a topic from terms with the Taxonomy API, you need to have edit permissions to the desired term sets or terms. Additionally, for topic creation to succeed, the taxonomy term must have at least one file tagged with it.

    References: