public static void BindFieldsToTermSetsFromXML(this Web web, XmlDocument xmlDoc) { XmlNodeList fields = xmlDoc.SelectNodes("//MMSField"); foreach (XmlNode mmsfield in fields) { string fieldGuid = mmsfield.Attributes["FieldGuid"].Value; string MMSGroupName = mmsfield.Attributes["MMSGroupName"].Value; string TermSet = mmsfield.Attributes["TermSet"].Value; TaxonomyExtensions.WireUpTaxonomyField(web, new Guid(fieldGuid), MMSGroupName, TermSet); } }
/// <summary> /// /// </summary> /// <param name="values"></param> /// <param name="log"></param> /// <param name="ctx"></param> /// <param name="item"></param> /// <param name="field"></param> private static void UpdateManagedMetadata(string[] values, TraceWriter log, ClientContext ctx, ListItem item, Field field, Guid guid) { try { var taxSession = ctx.Site.GetTaxonomySession(); var terms = new List <KeyValuePair <Guid, string> >(); var store = TaxonomyExtensions.GetDefaultKeywordsTermStore(ctx.Site); //var keywordTermSet = store.KeywordsTermSet; var keywordTermSet = store.GetTermSet(guid); ctx.Load(field); ctx.ExecuteQuery(); TaxonomyField taxField = ctx.CastTo <TaxonomyField>(field); taxField.EnsureProperty(tf => tf.AllowMultipleValues); ctx.Load(taxSession); ctx.Load(store); ctx.Load(taxField); ctx.Load(keywordTermSet); ctx.Load(item); ctx.ExecuteQuery(); taxField.IsKeyword = false; //taxField.TermSetId = keywordTermSet.Id; taxField.Update(); ctx.Load(taxField); ctx.ExecuteQuery(); foreach (var arrayItem in values) { Term term1 = null; Guid termGuid = Guid.Empty; term1 = keywordTermSet.Terms.GetByName(arrayItem); // Test if this term is available try { ctx.Load(term1); ctx.ExecuteQuery(); } catch (Exception ex) { log.Info("Encountered an error, probably because a term didn't exist yet! Creating..."); term1 = keywordTermSet.CreateTerm(arrayItem, lcid, Guid.NewGuid()); term1.IsAvailableForTagging = true; } ctx.Load(term1); ctx.ExecuteQuery(); store.CommitAll(); ctx.ExecuteQuery(); terms.Add(new KeyValuePair <Guid, string>(term1.Id, term1.Name)); } ctx.Load(item); ctx.ExecuteQuery(); TaxonomyExtensions.SetTaxonomyFieldValues(item, taxField.Id, terms); item.Update(); } catch (Exception ex) { log.Error(ex.Message); } }