/// <summary> /// Helper Methods to get a TermId by a Name /// </summary> /// <param name="ctx">The Authenticated ClientContext</param> /// <param name="term">The Term Name do lookup.</param> /// <param name="termSetId">The TermSet Guid</param> /// <returns></returns> public static string GetTermIdByName(ClientContext ctx, string term, Guid termSetId) { string _resultTerm = string.Empty; var _taxSession = TaxonomySession.GetTaxonomySession(ctx); var _termStore = _taxSession.GetDefaultSiteCollectionTermStore(); var _termSet = _termStore.GetTermSet(termSetId); var _termMatch = new LabelMatchInformation(ctx) { Lcid = 1033, TermLabel = term, TrimUnavailable = true }; var _termCollection = _termSet.GetTerms(_termMatch); ctx.Load(_taxSession); ctx.Load(_termStore); ctx.Load(_termSet); ctx.Load(_termCollection); ctx.ExecuteQuery(); if (_termCollection.Count() > 0) _resultTerm = _termCollection.First().Id.ToString(); return _resultTerm; }
/// <summary> /// Helper Methods to get a TermId by a Name /// </summary> /// <param name="cc">The Authenticated ClientContext</param> /// <param name="term">The Term Name do lookup.</param> /// <param name="termSetId">The TermSet Guid</param> /// <returns></returns> public static string GetTermIdByName(ClientContext cc, string term, Guid termSetId) { string _resultTerm = string.Empty; var _taxSession = TaxonomySession.GetTaxonomySession(cc); var _termStore = _taxSession.GetDefaultSiteCollectionTermStore(); var _termSet = _termStore.GetTermSet(termSetId); var _termMatch = new LabelMatchInformation(cc) { Lcid = 1033, TermLabel = term, TrimUnavailable = true }; var _termCollection = _termSet.GetTerms(_termMatch); cc.Load(_taxSession); cc.Load(_termStore); cc.Load(_termSet); cc.Load(_termCollection); cc.ExecuteQuery(); if (_termCollection.Count() > 0) { _resultTerm = _termCollection.First().Id.ToString(); } return(_resultTerm); }
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); }
public static string GetTermIdForTerm(string term, Guid termSetId, ClientContext clientContext) { //Ref: Steve Curran - http://sharepointfieldnotes.blogspot.com/2013_06_01_archive.html 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); }
static void Main(string[] args) { ClientContext context = new ClientContext("http://ecm.wingtip.com"); TaxonomySession session = TaxonomySession.GetTaxonomySession(context); context.Load(session, taxSession => taxSession.TermStores.Include( taxStore => taxStore.Groups.Include( taxGroup => taxGroup.TermSets.Include(tax => tax.Name) ))); context.ExecuteQuery(); TermStore termStore = session.TermStores[0]; TermGroup termGroup = termStore.Groups[0]; TermSet termSet = termGroup.TermSets[0]; //// get UNITED STATES term //var terms = termSet.Terms; //context.Load(terms); //context.ExecuteQuery(); //Term unitedStatesTerm = terms[0]; //context.Load(unitedStatesTerm); //context.ExecuteQuery(); //// add region //Term newRegion = unitedStatesTerm.CreateTerm("Pacific", 1033, Guid.NewGuid()); //newRegion.SetCustomProperty("PrimaryPOC", "Rob Walters"); //newRegion.IsAvailableForTagging = false; //// add state //Term newState = newRegion.CreateTerm("Hawaii", 1033, Guid.NewGuid()); // search for PACIFIC term var searchQuery = new LabelMatchInformation(context) { TermLabel = "Pacific", TrimUnavailable = false }; var foundTerms = termSet.GetTerms(searchQuery); context.Load(foundTerms); context.ExecuteQuery(); // update term foundTerms[0].Name = "Pacific Region"; // save changes termStore.CommitAll(); context.ExecuteQuery(); }
public Term GetTerm(ClientContext clientContext, TermStore termStore, TermSet termSet, bool recursive, Expression <Func <Term, object> >[] expressions = null) { Term term = null; if (_id != Guid.Empty) { term = termStore.GetTerm(_id); } else if (!string.IsNullOrEmpty(_title) && termSet != null && termStore != null) { var termName = TaxonomyExtensions.NormalizeName(_title); if (!recursive) { term = termSet.Terms.GetByName(termName); } else { var lmi = new LabelMatchInformation(clientContext) { TrimUnavailable = true, TermLabel = termName }; var termMatches = termSet.GetTerms(lmi); clientContext.Load(termMatches); clientContext.ExecuteQueryRetry(); if (termMatches.AreItemsAvailable) { term = termMatches.FirstOrDefault(); } } } else { throw new PSArgumentException("Not enough parameters specified to succesfully find the term"); } if (expressions != null) { clientContext.Load(term, expressions); } else { clientContext.Load(term); } clientContext.ExecuteQueryRetry(); return(term); }
private static Guid AddKeyWord(string label, ClientContext ctx) { if (string.IsNullOrEmpty(label)) { return(Guid.Empty); } LogHelper.Log("label : " + label + " ctx:" + ctx); TaxonomySession tax = TaxonomySession.GetTaxonomySession(ctx); Guid TermId = Guid.NewGuid(); try { tax.GetDefaultKeywordsTermStore().KeywordsTermSet.CreateTerm(label, 1033, TermId); ctx.ExecuteQuery(); EmptyKeywordsNeedsRefresh = true; return(TermId); } catch (Exception ex) { LogHelper.Log("failed for label : " + label + " ctx:" + ctx + " tax:" + tax, LogSeverity.Error); try { //cannot reuse tax because it is stale. TaxonomySession tax1 = TaxonomySession.GetTaxonomySession(ctx); LabelMatchInformation m = new LabelMatchInformation(ctx); m.TermLabel = label; m.TrimUnavailable = true; TermCollection matches = tax1.GetDefaultKeywordsTermStore().KeywordsTermSet.GetTerms(m); ctx.Load(matches); ctx.ExecuteQuery(); if (matches.Count > 0) { return(matches[0].Id); } else { return(Guid.Empty); } } catch (Exception subex) { LogHelper.Log(subex.Message + subex.StackTrace, LogSeverity.Error); return(Guid.Empty); } } }
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); }
/// <summary> /// Gets a Taxonomy Term by Name /// </summary> /// <param name="termSetId"></param> /// <param name="term"></param> /// <param name="clientContext"></param> /// <returns></returns> public static Term GetTermByName(this Site site, Guid termSetId, string term) { if (string.IsNullOrEmpty(term)) { throw new ArgumentNullException("term"); } TermCollection termMatches = null; ExceptionHandlingScope scope = new ExceptionHandlingScope(site.Context); string termId = string.Empty; TaxonomySession tSession = TaxonomySession.GetTaxonomySession(site.Context); TermStore ts = tSession.GetDefaultSiteCollectionTermStore(); TermSet tset = ts.GetTermSet(termSetId); var lmi = new LabelMatchInformation(site.Context); lmi.Lcid = 1033; lmi.TrimUnavailable = true; lmi.TermLabel = term; termMatches = tset.GetTerms(lmi); site.Context.Load(tSession); site.Context.Load(ts); site.Context.Load(tset); site.Context.Load(termMatches); site.Context.ExecuteQuery(); if (termMatches.AreItemsAvailable) { return(termMatches.FirstOrDefault()); } else { return(null); } }
private void UpdateTaxonomyField(ClientContext ctx, List list, ListItem item) { //Empezamos inicialización de término //Obtenemos el término asociado para obtener el id var taxonomySession = TaxonomySession.GetTaxonomySession(ctx); var store = taxonomySession.GetDefaultSiteCollectionTermStore(); var group = store.GetSiteCollectionGroup(ctx.Site, false); ctx.Load(group); ctx.Load(group.TermSets); ctx.ExecuteQuery(); //Aquí ha de apuntar a vuestro almacén de términos: Paises var termSet = group.TermSets.FirstOrDefault(x => x.Name == Constants.Taxonomy.Pais); LabelMatchInformation labelMatch = new LabelMatchInformation(ctx) { TermLabel = this.Provincia, Lcid = 1033, TrimUnavailable = true }; var terms = termSet.GetTerms(labelMatch); ctx.Load(terms); ctx.ExecuteQuery(); var taxonomyProvincia = terms.First(x => x.Name == this.Provincia); //Obtenemos el campo asociado en el listado para coger su ID var provinciaField = list.Fields.GetByInternalNameOrTitle(Constants.Columns.Pais); ctx.Load(provinciaField, x => x.Id); ctx.ExecuteQuery(); //Seteamos el campo, hace falta tener instalado el PnP de SharePoint item.SetTaxonomyFieldValue(provinciaField.Id, this.Provincia, taxonomyProvincia.Id); item.SystemUpdate(); ctx.ExecuteQuery(); }
/// <summary> /// Gets a Taxonomy Term by Name /// </summary> /// <param name="termSetId"></param> /// <param name="term"></param> /// <param name="clientContext"></param> /// <returns></returns> public static Term GetTermByName(this Site site, Guid termSetId, string term) { if (string.IsNullOrEmpty(term)) throw new ArgumentNullException("term"); TermCollection termMatches = null; ExceptionHandlingScope scope = new ExceptionHandlingScope(site.Context); string termId = string.Empty; TaxonomySession tSession = TaxonomySession.GetTaxonomySession(site.Context); TermStore ts = tSession.GetDefaultSiteCollectionTermStore(); TermSet tset = ts.GetTermSet(termSetId); var lmi = new LabelMatchInformation(site.Context); lmi.Lcid = 1033; lmi.TrimUnavailable = true; lmi.TermLabel = term; termMatches = tset.GetTerms(lmi); site.Context.Load(tSession); site.Context.Load(ts); site.Context.Load(tset); site.Context.Load(termMatches); site.Context.ExecuteQuery(); if (termMatches.AreItemsAvailable) { return termMatches.FirstOrDefault(); } else { return null; } }
private static Guid AddKeyWord(string label, ClientContext ctx) { if (string.IsNullOrEmpty(label)) return Guid.Empty; LogHelper.Log("label : " + label + " ctx:" + ctx); TaxonomySession tax = TaxonomySession.GetTaxonomySession(ctx); Guid TermId = Guid.NewGuid(); try { tax.GetDefaultKeywordsTermStore().KeywordsTermSet.CreateTerm(label, 1033, TermId); ctx.ExecuteQuery(); EmptyKeywordsNeedsRefresh = true; return TermId; } catch (Exception ex) { LogHelper.Log("failed for label : " + label + " ctx:"+ctx+" tax:"+tax, LogSeverity.Error); try { //cannot reuse tax because it is stale. TaxonomySession tax1 = TaxonomySession.GetTaxonomySession(ctx); LabelMatchInformation m = new LabelMatchInformation(ctx); m.TermLabel = label; m.TrimUnavailable = true; TermCollection matches=tax1.GetDefaultKeywordsTermStore().KeywordsTermSet.GetTerms(m); ctx.Load(matches); ctx.ExecuteQuery(); if (matches.Count > 0) { return matches[0].Id; } else return Guid.Empty; } catch (Exception subex) { LogHelper.Log(subex.Message+subex.StackTrace, LogSeverity.Error); return Guid.Empty; } } }
/// <summary> /// Gets a Taxonomy Term by Name /// </summary> /// <param name="termSetId"></param> /// <param name="term"></param> /// <param name="clientContext"></param> /// <returns></returns> public static Term GetTermByName(Guid termSetId, string term, ClientContext clientContext) { TermCollection termMatches = null; ExceptionHandlingScope scope = new ExceptionHandlingScope(clientContext); using (scope.StartScope()) { using (scope.StartTry()) { string termId = string.Empty; TaxonomySession tSession = TaxonomySession.GetTaxonomySession(clientContext); TermStore ts = tSession.GetDefaultSiteCollectionTermStore(); TermSet tset = ts.GetTermSet(termSetId); var lmi = new LabelMatchInformation(clientContext); lmi.Lcid = 1033; lmi.TrimUnavailable = true; lmi.TermLabel = term; termMatches = tset.GetTerms(lmi); clientContext.Load(tSession); clientContext.Load(ts); clientContext.Load(tset); clientContext.Load(termMatches); } using (scope.StartCatch()) { if (scope.HasException) { return null; } } } clientContext.ExecuteQuery(); if (termMatches.AreItemsAvailable) return termMatches.FirstOrDefault(); return null; }
private void Import(XmlElement termElement, TermSetItem parentTermSetItem) { string termName = termElement.GetAttribute("Name"); LoadWorkingLanguage(parentTermSetItem.TermStore); Term term = null; ExceptionHandlingScope scope = new ExceptionHandlingScope(_ctx); using (scope.StartScope()) { using (scope.StartTry()) { term = parentTermSetItem.Terms.GetByName(termName); _ctx.Load(term); } using (scope.StartCatch()) { } } _ctx.ExecuteQuery(); if (term == null || term.ServerObjectIsNull == null || term.ServerObjectIsNull.Value) { if (!string.IsNullOrEmpty(termElement.GetAttribute("IsSourceTerm")) && !bool.Parse(termElement.GetAttribute("IsSourceTerm"))) { string[] sourceTermInfo = termElement.GetAttribute("SourceTerm").Split('|'); Term sourceTerm = null; ExceptionHandlingScope scope1 = new ExceptionHandlingScope(_ctx); using (scope1.StartScope()) { using (scope1.StartTry()) { sourceTerm = parentTermSetItem.TermStore.GetTerm(new Guid(sourceTermInfo[0])); _ctx.Load(sourceTerm); } using (scope1.StartCatch()) { } } _ctx.ExecuteQuery(); if (sourceTerm == null || sourceTerm.ServerObjectIsNull == null || sourceTerm.ServerObjectIsNull.Value) { LabelMatchInformation lmi = new LabelMatchInformation(parentTermSetItem.Context); lmi.StringMatchOption = StringMatchOption.ExactMatch; lmi.DefaultLabelOnly = true; lmi.ResultCollectionSize = 1; lmi.TermLabel = sourceTermInfo[1]; lmi.TrimUnavailable = false; TermCollection sourceTerms = parentTermSetItem.TermStore.GetTerms(lmi); _ctx.ExecuteQuery(); if (sourceTerms != null && !sourceTerms.ServerObjectIsNull.Value && sourceTerms.Count > 0) { sourceTerm = sourceTerms[0]; } } if (sourceTerm != null) { _cmdlet.WriteVerbose(string.Format("Creating Reference Term: {0}", termName)); if (termElement.GetAttribute("IsPinnedRoot") != "False") { term = parentTermSetItem.ReuseTermWithPinning(sourceTerm); } //else // term = parentTermSetItem.ReuseTerm(sourceTerm, false); _ctx.ExecuteQuery(); } else { _cmdlet.WriteWarning(string.Format("The Source Term, {0}, was not found. {1} will be created without linking.", sourceTermInfo[1], termName)); } } if (term == null || term.ServerObjectIsNull == null || term.ServerObjectIsNull.Value) { _cmdlet.WriteVerbose(string.Format("Creating Term: {0}", termName)); int lcid = _workingLanguage; Guid id = Guid.NewGuid(); if (!string.IsNullOrEmpty(termElement.GetAttribute("Id"))) { id = new Guid(termElement.GetAttribute("Id")); } term = parentTermSetItem.CreateTerm(termName, lcid, id); _LocalNavTermSetId = id; //Feature 152 if (!string.IsNullOrEmpty(termElement.GetAttribute("CustomSortOrder"))) { term.CustomSortOrder = termElement.GetAttribute("CustomSortOrder"); } if (!string.IsNullOrEmpty(termElement.GetAttribute("IsAvailableForTagging"))) { term.IsAvailableForTagging = bool.Parse(termElement.GetAttribute("IsAvailableForTagging")); } if (!string.IsNullOrEmpty(termElement.GetAttribute("Owner"))) { term.Owner = termElement.GetAttribute("Owner"); } if (!string.IsNullOrEmpty(termElement.GetAttribute("IsDeprecated")) && bool.Parse(termElement.GetAttribute("IsDeprecated"))) { term.Deprecate(true); } _ctx.ExecuteQuery(); } } if (termElement.GetAttribute("IsPinned") != "True") { XmlNodeList descriptionNodes = termElement.SelectNodes("./Descriptions/Description"); if (descriptionNodes != null && descriptionNodes.Count > 0) { foreach (XmlElement descriptionElement in descriptionNodes) { term.SetDescription(descriptionElement.GetAttribute("Value"), int.Parse(descriptionElement.GetAttribute("Language"))); } _ctx.ExecuteQuery(); } XmlNodeList propertyNodes = termElement.SelectNodes("./CustomProperties/CustomProperty"); if (propertyNodes != null && propertyNodes.Count > 0) { foreach (XmlElement propertyElement in propertyNodes) { term.SetCustomProperty(propertyElement.GetAttribute("Name"), propertyElement.GetAttribute("Value")); } _ctx.ExecuteQuery(); } XmlNodeList localPropertyNodes = termElement.SelectNodes("./LocalCustomProperties/LocalCustomProperty"); if (localPropertyNodes != null && localPropertyNodes.Count > 0) { foreach (XmlElement propertyElement in localPropertyNodes) { term.SetLocalCustomProperty(propertyElement.GetAttribute("Name"), propertyElement.GetAttribute("Value")); } _ctx.ExecuteQuery(); } XmlNodeList labelNodes = termElement.SelectNodes("./Labels/Label"); if (labelNodes != null && labelNodes.Count > 0) { foreach (XmlElement labelElement in labelNodes) { string labelValue = labelElement.GetAttribute("Value"); int lcid = int.Parse(labelElement.GetAttribute("Language")); bool isDefault = bool.Parse(labelElement.GetAttribute("IsDefaultForLanguage")); var labels = term.GetAllLabels(lcid); parentTermSetItem.Context.Load(labels); parentTermSetItem.Context.ExecuteQuery(); Label label = labels.FirstOrDefault(currentLabel => currentLabel.Value == labelValue); if (label == null || label.ServerObjectIsNull.Value) { term.CreateLabel(labelValue, lcid, isDefault); parentTermSetItem.Context.ExecuteQuery(); } else { if (isDefault && !label.IsDefaultForLanguage) { label.SetAsDefaultForLanguage(); parentTermSetItem.Context.ExecuteQuery(); } } } } parentTermSetItem.TermStore.CommitAll(); _ctx.ExecuteQuery(); XmlNodeList termsNodes = termElement.SelectNodes("./Terms/Term"); if (termsNodes != null && termsNodes.Count > 0) { foreach (XmlElement childTermElement in termsNodes) { Import(childTermElement, term); } } } }
protected override void ExecuteCmdlet() { DefaultRetrievalExpressions = new Expression <Func <Term, object> >[] { g => g.Name, g => g.TermsCount, g => g.Id }; var taxonomySession = TaxonomySession.GetTaxonomySession(ClientContext); // Get Term Store TermStore termStore = null; if (TermStore == null) { termStore = taxonomySession.GetDefaultSiteCollectionTermStore(); } else { if (TermStore.StringValue != null) { termStore = taxonomySession.TermStores.GetByName(TermStore.StringValue); } else if (TermStore.IdValue != Guid.Empty) { termStore = taxonomySession.TermStores.GetById(TermStore.IdValue); } else { if (TermStore.Item != null) { termStore = TermStore.Item; } } } if (ParameterSetName == ParameterSet_TERM) { if (Identity.IdValue != Guid.Empty) { term = termStore.GetTerm(Identity.IdValue); ClientContext.Load(term, RetrievalExpressions); ClientContext.ExecuteQueryRetry(); if (IncludeChildTerms.IsPresent && term.TermsCount > 0) { LoadChildTerms(term); } WriteObject(term); } else { WriteError(new ErrorRecord(new Exception("Insufficient parameters"), "INSUFFICIENTPARAMETERS", ErrorCategory.SyntaxError, this)); } } else { TermGroup termGroup = null; if (TermGroup != null && TermGroup.Id != Guid.Empty) { termGroup = termStore.Groups.GetById(TermGroup.Id); } else if (TermGroup != null && !string.IsNullOrEmpty(TermGroup.Name)) { termGroup = termStore.Groups.GetByName(TermGroup.Name); } TermSet termSet = null; if (TermSet != null) { if (TermSet.Id != Guid.Empty) { termSet = termGroup.TermSets.GetById(TermSet.Id); } else if (!string.IsNullOrEmpty(TermSet.Title)) { termSet = termGroup.TermSets.GetByName(TermSet.Title); } else { termSet = TermSet.Item; } } if (Identity != null) { term = null; if (Identity.IdValue != Guid.Empty) { term = termStore.GetTerm(Identity.IdValue); } else { var termName = TaxonomyExtensions.NormalizeName(Identity.StringValue); if (!Recursive) { term = termSet.Terms.GetByName(termName); } else { var lmi = new LabelMatchInformation(ClientContext) { TrimUnavailable = true, TermLabel = termName }; var termMatches = termSet.GetTerms(lmi); ClientContext.Load(termMatches); ClientContext.ExecuteQueryRetry(); if (termMatches.AreItemsAvailable) { term = termMatches.FirstOrDefault(); } } } ClientContext.Load(term, RetrievalExpressions); ClientContext.ExecuteQueryRetry(); if (IncludeChildTerms.IsPresent && term.TermsCount > 0) { LoadChildTerms(term); } WriteObject(term); } else { var query = termSet.Terms.IncludeWithDefaultProperties(RetrievalExpressions); var terms = ClientContext.LoadQuery(query); ClientContext.ExecuteQueryRetry(); if (IncludeChildTerms.IsPresent) { foreach (var collectionTerm in terms) { if (collectionTerm.TermsCount > 0) { LoadChildTerms(collectionTerm); } } } WriteObject(terms, true); } } }
protected override void ExecuteCmdlet() { DefaultRetrievalExpressions = new Expression <Func <Term, object> >[] { g => g.Name, g => g.Id }; var taxonomySession = TaxonomySession.GetTaxonomySession(ClientContext); // Get Term Store TermStore termStore = null; if (TermStore == null) { termStore = taxonomySession.GetDefaultSiteCollectionTermStore(); } else { if (TermStore.StringValue != null) { termStore = taxonomySession.TermStores.GetByName(TermStore.StringValue); } else if (TermStore.IdValue != Guid.Empty) { termStore = taxonomySession.TermStores.GetById(TermStore.IdValue); } else { if (TermStore.Item != null) { termStore = TermStore.Item; } } } TermGroup termGroup = null; if (TermGroup.Id != Guid.Empty) { termGroup = termStore.Groups.GetById(TermGroup.Id); } else if (!string.IsNullOrEmpty(TermGroup.Name)) { termGroup = termStore.Groups.GetByName(TermGroup.Name); } TermSet termSet; if (TermSet.Id != Guid.Empty) { termSet = termGroup.TermSets.GetById(TermSet.Id); } else if (!string.IsNullOrEmpty(TermSet.Title)) { termSet = termGroup.TermSets.GetByName(TermSet.Title); } else { termSet = TermSet.Item; } if (Identity != null) { Term term = null; if (Identity.IdValue != Guid.Empty) { term = termSet.Terms.GetById(Identity.IdValue); } else { var termName = TaxonomyExtensions.NormalizeName(Identity.StringValue); if (!Recursive) { term = termSet.Terms.GetByName(termName); } else { var lmi = new LabelMatchInformation(ClientContext) { TrimUnavailable = true, TermLabel = termName }; var termMatches = termSet.GetTerms(lmi); ClientContext.Load(termMatches); ClientContext.ExecuteQueryRetry(); if (termMatches.AreItemsAvailable) { term = termMatches.FirstOrDefault(); } } } ClientContext.Load(term, RetrievalExpressions); ClientContext.ExecuteQueryRetry(); WriteObject(term); } else { var query = termSet.Terms.IncludeWithDefaultProperties(RetrievalExpressions); var terms = ClientContext.LoadQuery(query); ClientContext.ExecuteQueryRetry(); WriteObject(terms, true); } }
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; }
private void Import(XmlElement termElement, TermSetItem parentTermSetItem) { string termName = termElement.GetAttribute("Name"); LoadWorkingLanguage(parentTermSetItem.TermStore); Term term = null; ExceptionHandlingScope scope = new ExceptionHandlingScope(_ctx); using (scope.StartScope()) { using (scope.StartTry()) { term = parentTermSetItem.Terms.GetByName(termName); _ctx.Load(term); } using (scope.StartCatch()) { } } _ctx.ExecuteQuery(); if (term == null || term.ServerObjectIsNull == null || term.ServerObjectIsNull.Value) { if (!string.IsNullOrEmpty(termElement.GetAttribute("IsSourceTerm")) && !bool.Parse(termElement.GetAttribute("IsSourceTerm"))) { string[] sourceTermInfo = termElement.GetAttribute("SourceTerm").Split('|'); Term sourceTerm = null; ExceptionHandlingScope scope1 = new ExceptionHandlingScope(_ctx); using (scope1.StartScope()) { using (scope1.StartTry()) { sourceTerm = parentTermSetItem.TermStore.GetTerm(new Guid(sourceTermInfo[0])); _ctx.Load(sourceTerm); } using (scope1.StartCatch()) { } } _ctx.ExecuteQuery(); if (sourceTerm == null || sourceTerm.ServerObjectIsNull == null || sourceTerm.ServerObjectIsNull.Value) { LabelMatchInformation lmi = new LabelMatchInformation(parentTermSetItem.Context); lmi.StringMatchOption = StringMatchOption.ExactMatch; lmi.DefaultLabelOnly = true; lmi.ResultCollectionSize = 1; lmi.TermLabel = sourceTermInfo[1]; lmi.TrimUnavailable = false; TermCollection sourceTerms = parentTermSetItem.TermStore.GetTerms(lmi); _ctx.ExecuteQuery(); if (sourceTerms != null && !sourceTerms.ServerObjectIsNull.Value && sourceTerms.Count > 0) sourceTerm = sourceTerms[0]; } if (sourceTerm != null) { _cmdlet.WriteVerbose(string.Format("Creating Reference Term: {0}", termName)); term = parentTermSetItem.ReuseTerm(sourceTerm, false); _ctx.ExecuteQuery(); } else _cmdlet.WriteWarning(string.Format("The Source Term, {0}, was not found. {1} will be created without linking.", sourceTermInfo[1], termName)); } if (term == null || term.ServerObjectIsNull == null || term.ServerObjectIsNull.Value) { _cmdlet.WriteVerbose(string.Format("Creating Term: {0}", termName)); int lcid = _workingLanguage; Guid id = Guid.NewGuid(); if (!string.IsNullOrEmpty(termElement.GetAttribute("Id"))) id = new Guid(termElement.GetAttribute("Id")); term = parentTermSetItem.CreateTerm(termName, lcid, id); if (!string.IsNullOrEmpty(termElement.GetAttribute("CustomSortOrder"))) term.CustomSortOrder = termElement.GetAttribute("CustomSortOrder"); if (!string.IsNullOrEmpty(termElement.GetAttribute("IsAvailableForTagging"))) term.IsAvailableForTagging = bool.Parse(termElement.GetAttribute("IsAvailableForTagging")); if (!string.IsNullOrEmpty(termElement.GetAttribute("Owner"))) term.Owner = termElement.GetAttribute("Owner"); if (!string.IsNullOrEmpty(termElement.GetAttribute("IsDeprecated")) && bool.Parse(termElement.GetAttribute("IsDeprecated"))) term.Deprecate(true); _ctx.ExecuteQuery(); } } XmlNodeList descriptionNodes = termElement.SelectNodes("./Descriptions/Description"); if (descriptionNodes != null && descriptionNodes.Count > 0) { foreach (XmlElement descriptionElement in descriptionNodes) { term.SetDescription(descriptionElement.GetAttribute("Value"), int.Parse(descriptionElement.GetAttribute("Language"))); } _ctx.ExecuteQuery(); } XmlNodeList propertyNodes = termElement.SelectNodes("./CustomProperties/CustomProperty"); if (propertyNodes != null && propertyNodes.Count > 0) { foreach (XmlElement propertyElement in propertyNodes) { term.SetCustomProperty(propertyElement.GetAttribute("Name"), propertyElement.GetAttribute("Value")); } _ctx.ExecuteQuery(); } XmlNodeList localPropertyNodes = termElement.SelectNodes("./LocalCustomProperties/LocalCustomProperty"); if (localPropertyNodes != null && localPropertyNodes.Count > 0) { foreach (XmlElement propertyElement in localPropertyNodes) { term.SetLocalCustomProperty(propertyElement.GetAttribute("Name"), propertyElement.GetAttribute("Value")); } _ctx.ExecuteQuery(); } XmlNodeList labelNodes = termElement.SelectNodes("./Labels/Label"); if (labelNodes != null && labelNodes.Count > 0) { foreach (XmlElement labelElement in labelNodes) { string labelValue = labelElement.GetAttribute("Value"); int lcid = int.Parse(labelElement.GetAttribute("Language")); bool isDefault = bool.Parse(labelElement.GetAttribute("IsDefaultForLanguage")); var labels = term.GetAllLabels(lcid); parentTermSetItem.Context.Load(labels); parentTermSetItem.Context.ExecuteQuery(); Label label = labels.FirstOrDefault(currentLabel => currentLabel.Value == labelValue); if (label == null || label.ServerObjectIsNull.Value) { term.CreateLabel(labelValue, lcid, isDefault); parentTermSetItem.Context.ExecuteQuery(); } else { if (isDefault && !label.IsDefaultForLanguage) { label.SetAsDefaultForLanguage(); parentTermSetItem.Context.ExecuteQuery(); } } } } parentTermSetItem.TermStore.CommitAll(); _ctx.ExecuteQuery(); XmlNodeList termsNodes = termElement.SelectNodes("./Terms/Term"); if (termsNodes != null && termsNodes.Count > 0) { foreach (XmlElement childTermElement in termsNodes) { Import(childTermElement, term); } } }