0

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

1 Answers1

0

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);
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. **Would you kindly [edit] your answer to include additional details for the benefit of the community?** – Jeremy Caney Jun 30 '23 at 04:49