sharepoint-onlinecsomtaxonomy-termssharepointdocumentlibrary

SharePoint Online - Managed Metadata - TermSets - Document Library


Is there a way to get all managed metadata fields and associated termsets/ terms of that field of a specific document library in SharePoint Online using CSOM?

I can get all the terms/ termset from term store. I can loop through term group, term set and terms. However, I cannot find a way to see which fields are associated to which terms.

If I go through all terms from term store, Throttling comes in to play.

If there is a way just to find terms that is associated to a given document library, please point me to the right direction.

Thanks


Solution

  • If anyone is having the same problem I was having... Here is what I've done....

    Field field = list.Fields.GetByInternalNameOrTitle("Your column Name");
    clientContext.Load(field);
    clientContext.ExecuteQuery();
    
    var taxField = clientContext.CastTo<TaxonomyField>(field);
    clientContext.Load(taxField);
    clientContext.ExecuteQuery();
    
    // Get the term set ID from the field's property
    var termSetId = taxField.TermSetId;
    //Get the term set by its ID
    var taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
    var termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
    var termSet = termStore.GetTermSet(termSetId);
    var terms = termSet.GetAllTerms();
    clientContext.Load(termSet);
    clientContext.Load(terms);
    clientContext.ExecuteQuery();
    
    Term term = terms.GetByName("The value to assign");
    clientContext.Load(term);
    clientContext.ExecuteQuery();
    TaxonomyFieldValue taxonomyFieldValue = new TaxonomyFieldValue();
    taxonomyFieldValue.TermGuid = term.Id.ToString();
    taxonomyFieldValue.Label = term.Name;
    taxField.SetFieldValueByValue(item, taxonomyFieldValue);