SharePoint Online - Add/Update Taxonomy Field Using Graph API

Introduction

Updating SharePoint taxonomy fields using Microsoft Graph API opens up a world of possibilities for developers looking to automate and enhance their SharePoint environments. However, it can be challenging due to the limited support for managed metadata fields.

Here we can see how to update the taxonomy field using Microsoft Graph API. Ensure your Azure app application has the necessary permissions to access and update taxonomy fields in SharePoint.

Here I am using Microsoft Graph client (_graphServiceClient) to get the details from SharePoint. All requests are made with app-only permissions.

To access the hidden internal taxonomy fields, start by querying these fields from SharePoint with the following query options.

var libQueryOptions = new List<QueryOption>
{
    new QueryOption("expand", "hidden")
};

Retrieve the columns of a SharePoint list by specifying the siteId and listId, then include the query options to ensure the hidden fields are also returned.

var listColumns = await _graphServiceClient.Sites[siteId]
    .Lists[listId]
    .Columns
    .Request(libQueryOptions)
    .WithAppOnly()
    .GetAsync();

Initialize a dictionary to store managed metadata field values and iterate over fieldDictionary, which contains field names and values. For each field, it finds the corresponding column in listColumns by matching the display name (with a suffix _0). Adds an entry to taxonomyDictionary with the field ID and a formatted value ("-1;" + field.Value + "|" + field.Value). Assume we don't know the correct value of term so add term id instead of term name. (-1:termid|termid)

Dictionary<string, object> taxonomyDictionary = new Dictionary<string, object>();

foreach (var field in fieldDictionary)
{
    var fieldId = listColumns.FirstOrDefault(x => x.DisplayName == field.Key + "_0")?.Name ?? string.Empty;
    taxonomyDictionary.Add(fieldId, "-1;" + field.Value + "|" + field.Value);
}

Use the Microsoft Graph client to update the fields of a specific item in the SharePoint list. This will update the taxonomy field to the list item.

Full code

try
{
    Dictionary<string, string> fieldDictionary = new Dictionary<string, string>
    {
        { "TaxonomyFieldName", TermGuidValue ?? "" }
    };

    var libQueryOptions = new List<QueryOption>()
    {
        new QueryOption("expand", "hidden")
    };

    // Get Field Id from SharePoint list
    var listColumns = await _graphServiceClient.Sites[siteId]
        .Lists[listId]
        .Columns
        .Request(libQueryOptions)
        .WithAppOnly()
        .GetAsync();

    // Generate dictionary for managed metadata field
    Dictionary<string, object> taxonomyDictionary = new Dictionary<string, object>();
    foreach (var field in fieldDictionary)
    {
        var fieldId = listColumns.FirstOrDefault(x => x.DisplayName == field.Key + "_0")?.Name ?? string.Empty;
        taxonomyDictionary.Add(fieldId, "-1;" + field.Value + "|" + field.Value);
    }

    var managedMetadataFieldValueSet = new FieldValueSet
    {
        AdditionalData = taxonomyDictionary
    };

    await _graphServiceClient.Sites[siteId]
        .Lists[dossierLib.Id]
        .Items[dossierId]
        .Fields
        .Request()
        .WithAppOnly()
        .UpdateAsync(managedMetadataFieldValueSet);
}