Search code examples
microsoft-graph-apimicrosoft-graph-files

How to set a Term into the TaxKeyword field of a file with Microsoft Graph


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": {
        "[email protected]": "#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


Solution

  • This works for me for the column with the taxonomy property that allows only one value:

    First, you need to know the name of the hidden column associated with your column TaxKeyword

    GET https://graph.microsoft.com/v1.0/sites/{site_id}/lists/{list_id}/columns?$select=hidden,id,name,displayName
    

    Find a column TaxKeyword_0 and use its id in the PATCH request:

    PATCH https://graph.microsoft.com/v1.0/sites/{site_id}/lists/{list_id}/items/{item_id}/fields
    {
        "hidden_column_id": "-1;#'TermName'|{TermGuid}"
    }
    

    Also this should be valid request body

    {
        "hidden_column_id": "TermName|TermGuid"
    }
    

    Example of the body in my case:

    {
        "a2e29228210545d49caf74aef41dea60": "-1;#'senior software engineer'|{5e6730d0-86bf-4913-bfa3-f7369e5bade4}"
    }
    

    or

    {
      "a2e29228210545d49caf74aef41dea60": "DevOps|c1276249-3b86-49bd-af51-d89cc2deeac2"
    }
    

    If the column allows multiple values

    PATCH https://graph.microsoft.com/v1.0/sites/{site_id}/lists/{list_id}/items/{item_id}/fields
    {
        "hidden_column_id": "Term1|Term1Guid;Term2|Term2Guid"
    }
    

    Example of the body in my case:

    {
        "o739791951f4466caffa4beeee1091f2": "B_Mala|10e9cc83-b5a4-4c8d-8dab-4ada1252dd70;B_Zadni|2cae6c6a-9bb8-4a78-afff-81b88e735fef"
    }