/// <summary> /// Helper Method to set a Taxonomy Field on a list item /// </summary> /// <param name="ctx">The Authenticated ClientContext</param> /// <param name="listItem">The listitem to modify</param> /// <param name="model">Domain Object of key/value pairs of the taxonomy field & value</param> public static void SetTaxonomyField(ClientContext ctx, ListItem listItem, Hashtable model) { FieldCollection _fields = listItem.ParentList.Fields; ctx.Load(_fields); ctx.ExecuteQuery(); foreach (var _key in model.Keys) { var _termName = model[_key].ToString(); TaxonomyField _field = ctx.CastTo <TaxonomyField>(_fields.GetByInternalNameOrTitle(_key.ToString())); ctx.Load(_field); ctx.ExecuteQuery(); Guid _id = _field.TermSetId; string _termID = AutoTaggingHelper.GetTermIdByName(ctx, _termName, _id); var _termValue = new TaxonomyFieldValue() { Label = _termName, TermGuid = _termID, WssId = -1 }; _field.SetFieldValueByValue(listItem, _termValue); listItem.Update(); ctx.ExecuteQuery(); } }
public TaxonomyFieldValue GetTaxonomyField(ClientContext ctx, ListItem listItem, string fieldName, string term) { FieldCollection _fields = listItem.ParentList.Fields; ctx.Load(_fields); ctx.ExecuteQuery(); TaxonomyField _field = ctx.CastTo <TaxonomyField>(_fields.GetByInternalNameOrTitle(fieldName)); ctx.Load(_field); ctx.ExecuteQuery(); Guid _id = _field.TermSetId; string _termID = AutoTaggingHelper.GetTermIdByName(ctx, term, _id); var _termValue = new TaxonomyFieldValue() { Label = term, TermGuid = _termID, WssId = -1 }; return(_termValue); }