Document Library's Taxonomy field updating only intermittently.

Jas K 0 Reputation points
2023-07-12T03:01:08.3433333+00:00

Hello! I am trying to update a Document Library with few thousand documents in it. I have a Taxonomy lookup field which holds Terms "Years". Using CSOM, C#, I am able to update records in the list. What is strange it that only few records reflect the updated Taxonomy Term field, other records show no sign of updated value, it remains empty. Checking the version history for the records shows the record has moved a version up but no change has been recorded. I am even added a delay of 10 seconds between each record update, but the issue remains the same. Bottom line, there are hundreds of incidents where the record is properly updated but there are also records that do not reflect the change. Very inconsistent. Any help is appreciated. Thanks

Here is the method that is being used to update the field:

public static void UpdateTaxonomyFieldValue(string[] tags, List list, ListItem listItem, string fieldName, bool isSingle, List < Term > termList, bool cleanup)
{
    string msg;
    if(cleanup)
    {
        ClearTaxonomyFieldValue(list.Context, list, listItem, fieldName);
    }
    List < Term > selectedTerms = new List < Term > ();
    for(int x = 0; x < tags.Length; x++)
    {
        Term y = termList.Find(t => t.Name == tags[x]);
        if(y != null)
        {
            selectedTerms.Add(y);
        }
    }
    var tagsString = GetTermsString(selectedTerms);
    var clientRuntimeContext = listItem.Context;
    var field = list.Fields.GetByInternalNameOrTitle(fieldName);
    var taxKeywordField = clientRuntimeContext.CastTo < TaxonomyField > (field);
    if(isSingle)
    {
        TaxonomyFieldValue termValue = new TaxonomyFieldValue();
        string[] term = tagsString.Split('|');
        if(term[0] != "")
        {
            termValue.Label = term[0];
            termValue.TermGuid = term[1];
            termValue.WssId = -1;
            taxKeywordField.SetFieldValueByValue(listItem, termValue);
            msg = String.Format("Field:  {0} Tag Value: {1} ", fieldName, tagsString);
            LogWriter.LogWrite(msg, LogWriter.Option.Msg);
        }
        else
        {
            msg = String.Format("-----------------Error: Field:  {0} Tag Value: {1} ", fieldName, tagsString);
            LogWriter.LogWrite(msg, LogWriter.Option.Msg);
        }
    }
    else
    {
        taxKeywordField.SetFieldValueByValueCollection(listItem, new TaxonomyFieldValueCollection(clientRuntimeContext, tagsString, taxKeywordField));
        msg = String.Format("Field:  {0} Tag - Multiple Value: {1} ", fieldName, tagsString);
        LogWriter.LogWrite(msg, LogWriter.Option.Msg);
    }
    taxKeywordField.Update();
}
Microsoft 365 and Office SharePoint Development
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Jas K 0 Reputation points
    2023-07-12T13:41:55.1333333+00:00

    No errors at all. It all seems to work. Some records get updated but not all. Let say out of 1000 records that need to be updated, I may find 600+ showing the correct data update but rest with no change. I can manually update the record with no issue. Very frustrating. Thanks for your help.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.