public static string GetTermIdForTerm(string term, Guid termSetId, ClientContext clientContext) { string termId = string.Empty; //Get term set from ID TaxonomySession tSession = TaxonomySession.GetTaxonomySession(clientContext); TermStore ts = tSession.GetDefaultSiteCollectionTermStore(); TermSet tset = ts.GetTermSet(termSetId); LabelMatchInformation lmi = new LabelMatchInformation(clientContext); lmi.Lcid = 1033; lmi.TrimUnavailable = true; lmi.TermLabel = term; //Search for matching terms in the term set based on label TermCollection termMatches = tset.GetTerms(lmi); clientContext.Load(tSession); clientContext.Load(ts); clientContext.Load(tset); clientContext.Load(termMatches); clientContext.ExecuteQuery(); //Set term ID to first match if (termMatches != null && termMatches.Count() > 0) { termId = termMatches.First().Id.ToString(); } return(termId); }
private string GetTermIdForTaxonomyField(TaxonomyField field, string term, ListItem pendingItem, Microsoft.SharePoint.Client.File pendingFile) { if (_terms == null) { _terms = new Dictionary <string, IDictionary <string, string> >(); } if (!_terms.Keys.Contains(field.Title)) { _terms[field.Title] = new Dictionary <string, string>(); } if (_terms[field.Title].Keys.Contains(term)) { return(_terms[field.Title][term].ToString()); } var termId = string.Empty; //before we go forward,save pending item pendingItem.Update(); ctx.Load(pendingFile); ctx.ExecuteQuery(); TaxonomySession tSession = TaxonomySession.GetTaxonomySession(ctx); ctx.Load(tSession.TermStores); ctx.ExecuteQuery(); TermStore ts = tSession.TermStores.First(); TermSet tset = ts.GetTermSet(field.TermSetId); LabelMatchInformation lmi = new LabelMatchInformation(ctx); lmi.Lcid = 1033; lmi.TrimUnavailable = true; lmi.TermLabel = term; TermCollection termMatches = tset.GetTerms(lmi); ctx.Load(tSession); ctx.Load(ts); ctx.Load(tset); ctx.Load(termMatches); ctx.ExecuteQuery(); if (termMatches != null && termMatches.Count() > 0) { termId = termMatches.First().Id.ToString(); } _terms[field.Title][term] = termId; return(termId); }
public static string GetTermIdForTerm(string term, Guid termSetId, ClientContext clientContext) { string termId = string.Empty; TaxonomySession tSession = TaxonomySession.GetTaxonomySession(clientContext); TermStore ts = tSession.GetDefaultSiteCollectionTermStore(); TermSet tset = ts.GetTermSet(termSetId); LabelMatchInformation lmi = new LabelMatchInformation(clientContext); lmi.Lcid = 1033; lmi.TrimUnavailable = true; lmi.TermLabel = term; TermCollection termMatches = tset.GetTerms(lmi); //tset.CreateTerm() clientContext.Load(tSession); clientContext.Load(ts); clientContext.Load(tset); clientContext.Load(termMatches); clientContext.ExecuteQuery(); if (termMatches != null && termMatches.Count() > 0) { termId = termMatches.First().Id.ToString(); } else { Term TermAdd = tset.CreateTerm(term, 1033, Guid.NewGuid()); clientContext.ExecuteQuery(); clientContext.Load(TermAdd); clientContext.ExecuteQuery(); termId = TermAdd.Id.ToString(); } return(termId); }
private void AddTerms(TermCollection _termCollection, bool isroot) { LogHelper.Log("Isroot:" + isroot); if (_termCollection.Count() > 0) { foreach (var t in _termCollection) { if (t.CustomProperties.ContainsKey(Constants.AutoTaggable)) //redundant in case the whole termset is loaded but no other way { AddLabels(t); // in case of termset. if (!isroot && t.Terms.Count > 0) // in case of subterm { foreach (var tt in t.Terms) { if (tt.CustomProperties.ContainsKey(Constants.AutoTaggable)) { AddLabels(tt); } } } } else { if (!isroot && t.Terms.Count > 0) { foreach (var tt in t.Terms) { if (tt.CustomProperties.ContainsKey(Constants.AutoTaggable)) { AddLabels(tt); } } } } } } }
private void AddTerms(TermCollection _termCollection,bool isroot) { LogHelper.Log("Isroot:" + isroot); if (_termCollection.Count() > 0) { foreach (var t in _termCollection) { if (t.CustomProperties.ContainsKey(Constants.AutoTaggable))//redundant in case the whole termset is loaded but no other way { AddLabels(t); // in case of termset. if(!isroot && t.Terms.Count > 0) // in case of subterm { foreach(var tt in t.Terms) { if(tt.CustomProperties.ContainsKey(Constants.AutoTaggable)) { AddLabels(tt); } } } } else { if (!isroot && t.Terms.Count > 0) { foreach (var tt in t.Terms) { if (tt.CustomProperties.ContainsKey(Constants.AutoTaggable)) { AddLabels(tt); } } } } } } }