public Wrapper(TermSetItem item, Guid id, Guid parentId, string name) { Item = item; Id = id; ParentId = parentId; Name = name; }
private Composite returnHierarchy(TermSetItem tsi, int languageCode) { try { int[] s = (tsi.GetType() == typeof(TermSet))?new[] { 0 }:GetWSSIds(((Term)tsi).Id, ((Term)tsi).TermSet.Id); string labelName1 = (tsi.GetType() == typeof(TermSet))?tsi.Name:GetLabelValue((Term)tsi, languageCode); Composite cmpRoot = new Composite(tsi.Id, labelName1, labelName1); cmpRoot.wssid = s.Length > 0 ? s[0] : 0;; foreach (TermSetItem item in tsi.Terms) { int[] ls = GetWSSIds(((Term)item).Id, ((Term)item).TermSet.Id); if (item.Terms.Count > 0) { cmpRoot.Add(returnHierarchy(item, languageCode)); } else { string labelName = GetLabelValue((Term)item, languageCode); Leaf leaf = new Leaf(item.Id, labelName, labelName); leaf.wssid = ls.Length > 0 ? ls[0] : 0; cmpRoot.Add(leaf); } } return(cmpRoot); } catch (Exception w) { _isValidState = false; log.Error(string.Format("{0}.{1}", MethodBase.GetCurrentMethod().DeclaringType.FullName, MethodBase.GetCurrentMethod().Name), w); throw; } }
private void AddTermsToConfig(ClientContext context, TermSetItem spTerm, ShTermSetItem shTermSetItem) { context.Load(spTerm, t => t.Terms.Include( item => item.Id, item => item.IsAvailableForTagging, item => item.IsDeprecated, item => item.IsReused, item => item.Description, item => item.Name, item => item.LocalCustomProperties, item => item.Labels)); context.ExecuteQuery(); foreach (var spChildTerm in spTerm.Terms) { var shTerm = new ShTerm(spChildTerm.Id, spChildTerm.Name); foreach (var label in spChildTerm.Labels) { shTerm.Labels.Add(new ShTermLabel { Language = label.Language, Value = label.Value, IsDefaultForLanguage = label.IsDefaultForLanguage }); } shTerm.LocalCustomProperties = spChildTerm.LocalCustomProperties; AddTermsToConfig(context, spChildTerm, shTerm); shTermSetItem.Terms.Add(shTerm); } }
// STEP 1: Add missing terms, and move existing terms to the correct // place in the tree, and possibly introducing temporary names // to avoid sibling conflicts. static void ProcessAddsAndMoves(TermSetItem parentItem, ItemGoal parentItemGoal, Dictionary <Guid, Term> termsById) { foreach (TermGoal termGoal in parentItemGoal.TermGoals) { // Does the term already exist? Term term; if (termsById.TryGetValue(termGoal.Id, out term)) { // The term exists, so ensure that it is a child of parentItem. Guid termParentId = (term.Parent != null) ? term.Parent.Id : term.TermSet.Id; if (termParentId != parentItem.Id) { MoveToParentItem(term, parentItem); } else { Log("Verified position of term \"" + term.Name + "\""); } } else { Log("* Creating missing term \"" + termGoal.Name + "\""); string safeName = GenerateNameWithoutSiblingConflicts(parentItem, termGoal.Name); term = parentItem.CreateTerm(safeName, lcid, termGoal.Id); parentItem.TermStore.CommitAll(); termsById.Add(term.Id, term); } ProcessAddsAndMoves(term, termGoal, termsById); // recurse } }
private static void UpdateTerm(TermSetItem termSet, ManagedMetadataEntity entity, IEnumerable <IEnumerable <ManagedMetadataEntity> > termLevels, int currLevel) { int lcid = entity.LCID.HasValue ? entity.LCID.Value : 1033; string termName = TaxonomyItem.NormalizeName(entity.GetTermLevel(currLevel)); if (string.IsNullOrEmpty(termName)) { return; } Term term = termSet is TermSet ? (termSet as TermSet).GetTerms(termName, lcid, true, StringMatchOption.ExactMatch, 1, false). FirstOrDefault() : termSet.Terms.FirstOrDefault(t => t.Name.Equals(termName, StringComparison.InvariantCulture)); if (term == null) { //try //{ term = termSet.CreateTerm(termName, lcid, Guid.NewGuid()); term.IsAvailableForTagging = entity.AvailableForTagging; //} //catch (Exception ex) //{ //} } LabelCollection allLabels = term.GetAllLabels(lcid); if (allLabels.Count == 0 || !allLabels.Select(x => x.Value).Contains(termName)) { //try //{ term.CreateLabel(termName, lcid, true); term.SetDescription(entity.TermDescription, lcid); //} //catch (Exception ex) //{ //} } if (termLevels == null) { return; } if (currLevel >= termLevels.Count()) { return; } IEnumerable <ManagedMetadataEntity> childList = termLevels.ElementAt(currLevel).Where( t => t.HasSameLevelTerm(currLevel, entity)); foreach (ManagedMetadataEntity childEntity in childList) { UpdateTerm(term, childEntity, termLevels, currLevel + 1); } }
// STEP 3: Update the term properties, including correcting any // temporary names that were introduced in step 1. static void ProcessPropertyUpdates(TermSetItem parentItem, ItemGoal parentItemGoal) { foreach (Term term in parentItem.Terms) { TermGoal termGoal = parentItemGoal.TermGoals.FirstOrDefault(t => t.Id == term.Id); if (termGoal == null) { continue; // This is a term that would have been deleted by the ProcessDeletes() method. } // ----------------- string goalName = TaxonomyItem.NormalizeName(termGoal.Name); if (term.Name != goalName) { Log("* Renaming term from \"" + term.Name + "\" to \"" + termGoal.Name + "\""); term.Name = goalName; } HashSet <string> labels = new HashSet <string>( term.Labels.Where(l => !l.IsDefaultForLanguage).Select(l => l.Value)); HashSet <string> labelsGoal = new HashSet <string>(termGoal.OtherLabels); // Delete any extra labels. foreach (string label in labels.Except(labelsGoal)) { Log("* Term \"" + term.Name + "\": Deleting label \"" + label + "\""); term.Labels.First(l => l.Value == label).Delete(); } // Add any missing labels. foreach (string label in labelsGoal.Except(labels)) { Log("* Term \"" + term.Name + "\": Adding label \"" + label + "\""); term.CreateLabel(label, lcid, isDefault: false); } if (term.GetDescription() != termGoal.Description) { Log("* Term \"" + term.Name + "\": Updating description"); term.SetDescription(termGoal.Description, lcid); } if (term.IsDeprecated != termGoal.IsDeprecated) { Log("* Term \"" + term.Name + "\": Marking as " + (termGoal.IsDeprecated ? "Deprecated" : "Not deprecated")); term.Deprecate(termGoal.IsDeprecated); } // ----------------- ProcessPropertyUpdates(term, termGoal); // Recurse. } }
public SPOTermSetItem(TermSetItem termSetItem) : base(termSetItem) { _termSetItem = termSetItem; if (_termSetItem.IsPropertyAvailable("CustomSortOrder")) SetProp("CustomSortOrder", _termSetItem.CustomSortOrder, false); if (_termSetItem.IsPropertyAvailable("IsAvailableForTagging")) SetProp("IsAvailableForTagging", _termSetItem.IsAvailableForTagging, false); if (_termSetItem.IsPropertyAvailable("Owner")) SetProp("Owner", _termSetItem.Owner, false); }
private void AddTermsToConfig(ClientContext context, TermSetItem spTerm, ShTermSetItem termItem) { context.Load(spTerm, t => t.Terms); context.ExecuteQuery(); foreach (var spChildTerm in spTerm.Terms) { var shTerm = new ShTerm(spChildTerm.Id, spChildTerm.Name); AddTermsToConfig(context, spChildTerm, shTerm); termItem.Terms.Add(shTerm); } }
// For a Term object, if the intendedName is already being used by a sibling Term object, // then the following method generates a unique name to avoid a conflict. static string GenerateNameWithoutSiblingConflicts(TermSetItem itemParent, string name) { foreach (Term sibling in itemParent.Terms) { if (sibling.Name == name) { string safeName = name + " " + Guid.NewGuid().ToString(); Log("Sibling conflict detected -- introducing temporary name \"" + safeName + "\""); return(safeName); } } return(name); // The name is okay. }
public static Term GetByName(this TermSetItem termSet, string name) { if (String.IsNullOrEmpty(name)) { throw new ArgumentException("Term set name cannot be empty", "name"); } Term term = termSet is TermSet ? (termSet as TermSet).GetTerms(name, true, StringMatchOption.ExactMatch, 1, false).SingleOrDefault() : termSet.Terms.GetByName(name); return(term); }
private void CreateAndAddToDict(TermSetItem parent, Dictionary <string, Term> termDict, SimpleTree <string> node, int lcid) { Term t = parent.CreateTerm(node.Value, lcid); if (string.IsNullOrEmpty(node.Description) == false) { t.SetDescription(node.Description, lcid); } if (node.CustomProperties != null) { foreach (var termProperty in node.CustomProperties) { t.SetCustomProperty(termProperty.Name, termProperty.Value); } } termDict.Add(node.Key, t); }
public SPOTermSetItem(TermSetItem termSetItem) : base(termSetItem) { _termSetItem = termSetItem; if (_termSetItem.IsPropertyAvailable("CustomSortOrder")) { SetProp("CustomSortOrder", _termSetItem.CustomSortOrder, false); } if (_termSetItem.IsPropertyAvailable("IsAvailableForTagging")) { SetProp("IsAvailableForTagging", _termSetItem.IsAvailableForTagging, false); } if (_termSetItem.IsPropertyAvailable("Owner")) { SetProp("Owner", _termSetItem.Owner, false); } }
// STEP 2: Delete any leftover terms. static void ProcessDeletes(TermSetItem parentItem, ItemGoal parentItemGoal) { foreach (Term term in parentItem.Terms) { TermGoal termGoal = parentItemGoal.TermGoals.FirstOrDefault(t => t.Id == term.Id); if (termGoal == null) { Log("* Deleting extra term \"" + term.Name + "\""); term.Delete(); term.TermStore.CommitAll(); } else { ProcessDeletes(term, termGoal); // recurse } } }
private TermSetItem FIllTaxonomyLOV(string TermstoreName, Guid TermsId, Guid termGroupId) { try { _TermStore = _TaxonomySession.TermStores[TermstoreName]; if (_TermStore == null) { _isValidState = false; throw (new Exception(String.Format("No Termstore Found Specified Name {0}", TermstoreName))); } TermSetItem tc = getTerCollByID(TermsId, TermstoreName, termGroupId); return(tc); } catch (Exception w) { _isValidState = false; log.Error(string.Format("{0}.{1}", MethodBase.GetCurrentMethod().DeclaringType.FullName, MethodBase.GetCurrentMethod().Name), w); throw; } }
static void MoveToParentItem(Term term, TermSetItem newParentItem) { Log("* Moving Term \"" + term.Name + "\" to be a child of \"" + newParentItem.Name + "\""); // Avoid key violations that are introduced by the move. string safeName = GenerateNameWithoutSiblingConflicts(newParentItem, term.Name); if (term.Name != safeName) { term.Name = safeName; } if (newParentItem is TermSet) { term.Move((TermSet)newParentItem); } else { term.Move((Term)newParentItem); } term.TermStore.CommitAll(); }
// This is a recursive function that creates a Term object // corresponding to each TermGoal object read from the input file. static void ImportTerms(TermSetItem parentItem, ItemGoal parentItemGoal) { foreach (TermGoal termGoal in parentItemGoal.TermGoals) { Log("Creating term \"" + termGoal.Name + "\""); Term term = parentItem.CreateTerm(termGoal.Name, lcid, termGoal.Id); term.Name = termGoal.Name; foreach (string label in termGoal.OtherLabels) { term.CreateLabel(label, lcid, isDefault: false); } term.SetDescription(termGoal.Description, lcid); if (termGoal.IsDeprecated) { term.Deprecate(true); } ImportTerms(term, termGoal); // recurse } }
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); } } } }
private void Import(XmlElement termElement, TermSetItem parentTermSetItem) { string termName = termElement.GetAttribute("Name"); Term term = null; try { term = parentTermSetItem.Terms[termName]; } catch (ArgumentException) {} if (term == null) { if (!string.IsNullOrEmpty(termElement.GetAttribute("IsSourceTerm")) && !bool.Parse(termElement.GetAttribute("IsSourceTerm"))) { string[] sourceTermInfo = termElement.GetAttribute("SourceTerm").Split('|'); Term sourceTerm = parentTermSetItem.TermStore.GetTerm(new Guid(sourceTermInfo[0])); if (sourceTerm == null) { TermCollection sourceTerms = parentTermSetItem.TermStore.GetTerms(sourceTermInfo[1], true, StringMatchOption.ExactMatch, 1, false); if (sourceTerms != null && sourceTerms.Count > 0) { sourceTerm = sourceTerms[0]; } } if (sourceTerm != null) { Logger.Write("Creating Reference Term: {0}", termName); term = parentTermSetItem.ReuseTerm(sourceTerm, false); } else { Logger.WriteWarning("The Source Term, {0}, was not found. {1} will be created without linking.", sourceTermInfo[1], termName); } } if (term == null) { Logger.Write("Creating Term: {0}", termName); int lcid = parentTermSetItem.TermStore.WorkingLanguage; if (!string.IsNullOrEmpty(termElement.GetAttribute("Id"))) { Guid id = new Guid(termElement.GetAttribute("Id")); term = parentTermSetItem.CreateTerm(termName, lcid, id); } else { term = parentTermSetItem.CreateTerm(termName, lcid); } 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); } } } 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"))); } } 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")); } } #if !SP2010 // Updated provided by John Calvert XmlNodeList localpropertyNodes = termElement.SelectNodes("./LocalCustomProperties/LocalCustomProperty"); if (localpropertyNodes != null && localpropertyNodes.Count > 0) { foreach (XmlElement localpropertyElement in localpropertyNodes) { term.SetLocalCustomProperty(localpropertyElement.GetAttribute("Name"), localpropertyElement.GetAttribute("Value")); } } // End update #endif 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")); Label label = term.GetAllLabels(lcid).FirstOrDefault(currentLabel => currentLabel.Value == labelValue); if (label == null) { term.CreateLabel(labelValue, lcid, isDefault); } else { if (isDefault && !label.IsDefaultForLanguage) { label.SetAsDefaultForLanguage(); } } } } parentTermSetItem.TermStore.CommitAll();//TEST XmlNodeList termsNodes = termElement.SelectNodes("./Terms/Term"); if (termsNodes != null && termsNodes.Count > 0) { foreach (XmlElement childTermElement in termsNodes) { Import(childTermElement, term); } } }
private void CreateTerm(ClientContext context, TermStore termStore, ShTerm shTerm, TermSetItem parentTerm) { var spTerm = termStore.GetTerm(shTerm.Id); context.Load(spTerm, t => t.Terms); context.ExecuteQuery(); if (spTerm.ServerObjectIsNull != null && spTerm.ServerObjectIsNull.Value) { spTerm = parentTerm.CreateTerm(shTerm.Title, termStore.DefaultLanguage, shTerm.Id); if (!string.IsNullOrEmpty(shTerm.CustomSortOrder)) { spTerm.CustomSortOrder = shTerm.CustomSortOrder; } spTerm.IsAvailableForTagging = !shTerm.NotAvailableForTagging; foreach (var prop in shTerm.CustomProperties) { spTerm.SetCustomProperty(prop.Key, prop.Value); } foreach (var prop in shTerm.LocalCustomProperties) { spTerm.SetLocalCustomProperty(prop.Key, prop.Value); } context.Load(spTerm); context.ExecuteQuery(); } foreach (ShTerm childTerm in shTerm.Terms) { CreateTerm(context, termStore, childTerm, spTerm); } }
private void Import(XmlElement termElement, TermSetItem parentTermSetItem) { string termName = termElement.GetAttribute("Name"); Term term = null; try { term = parentTermSetItem.Terms[termName]; } catch (ArgumentException) {} if (term == null) { if (!string.IsNullOrEmpty(termElement.GetAttribute("IsSourceTerm")) && !bool.Parse(termElement.GetAttribute("IsSourceTerm"))) { string[] sourceTermInfo = termElement.GetAttribute("SourceTerm").Split('|'); Term sourceTerm = parentTermSetItem.TermStore.GetTerm(new Guid(sourceTermInfo[0])); if (sourceTerm == null) { TermCollection sourceTerms = parentTermSetItem.TermStore.GetTerms(sourceTermInfo[1], true, StringMatchOption.ExactMatch, 1, false); if (sourceTerms != null && sourceTerms.Count > 0) sourceTerm = sourceTerms[0]; } if (sourceTerm != null) { Logger.Write("Creating Reference Term: {0}", termName); term = parentTermSetItem.ReuseTerm(sourceTerm, false); // Update provided by GSoft // Even if we re-use the source term, the term reuse may have a CustomSortOrder of its own, // so it's important to initialize it. The CustomSortOrder was aleady properly exported by Export-SPTerms, // so it was just strange not to allow its re-importation. if (!string.IsNullOrEmpty(termElement.GetAttribute("CustomSortOrder"))) { term.CustomSortOrder = termElement.GetAttribute("CustomSortOrder"); } // End update } else Logger.WriteWarning("The Source Term, {0}, was not found. {1} will be created without linking.", sourceTermInfo[1], termName); } if (term == null) { Logger.Write("Creating Term: {0}", termName); int lcid = parentTermSetItem.TermStore.WorkingLanguage; if (!string.IsNullOrEmpty(termElement.GetAttribute("Id"))) { Guid id = new Guid(termElement.GetAttribute("Id")); term = parentTermSetItem.CreateTerm(termName, lcid, id); } else term = parentTermSetItem.CreateTerm(termName, lcid); 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); } } 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"))); } } 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")); } } #if SP2013 // Update provided by GSoft // Rationale for deleting all LocalCustomProperties before adding those found in XML: // We may be initializing a term re-use. When you call 'parentTermSetItem.ReuseTerm(sourceTerm, false);' above, // the SharePoint API will copy the source term's LocalCustomProperties. However, this means that if the // source term had, for example, a _Sys_Nav_FriendlyUrlSegment value, then the term re-use will end up // with the same customized FriendlyUrlSegment. This is usually wrong, since the absence of a _Sys_Nav_FriendlyUrlSegment // property in the re-use's XML means that we don't want a customized friendly URL segment for that re-used term. // In other words, the only LocalCustomProperties added to a term re-use imported from XML should be the LocalCustomProperties // found in the XML, not those copied over from the source term during the ReuseTerm operation. term.DeleteAllLocalCustomProperties(); // End update // Updated provided by John Calvert XmlNodeList localpropertyNodes = termElement.SelectNodes("./LocalCustomProperties/LocalCustomProperty"); if (localpropertyNodes != null && localpropertyNodes.Count > 0) { foreach (XmlElement localpropertyElement in localpropertyNodes) { term.SetLocalCustomProperty(localpropertyElement.GetAttribute("Name"), localpropertyElement.GetAttribute("Value")); } } // End update #endif 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")); Label label = term.GetAllLabels(lcid).FirstOrDefault(currentLabel => currentLabel.Value == labelValue); if (label == null) { term.CreateLabel(labelValue, lcid, isDefault); } else { if (isDefault && !label.IsDefaultForLanguage) label.SetAsDefaultForLanguage(); } } } parentTermSetItem.TermStore.CommitAll();//TEST XmlNodeList termsNodes = termElement.SelectNodes("./Terms/Term"); if (termsNodes != null && termsNodes.Count > 0) { foreach (XmlElement childTermElement in termsNodes) { Import(childTermElement, term); } } }
// If we are creating a reused instance of a term, this locates the source term // (i.e. this.clientTermOtherInstance). private bool LoadClientTermOtherInstanceForTermLink() { if (this.localTerm.TermKind == LocalTermKind.TermLinkUsingId) { // Is the source term something that we're supposed to be creating in this data set? var localTermStore = this.Controller.LocalTermStore; var localSourceTerm = this.localTerm.SourceTerm; if (localSourceTerm != null) { TermUploader sourceTermUploader = (TermUploader)this.Controller.GetUploader(localSourceTerm); if (!sourceTermUploader.FoundClientObject) { this.WaitForBlocker(sourceTermUploader); return(false); } this.clientTermOtherInstance = sourceTermUploader.ClientTerm; } } else { Debug.Assert(this.localTerm.TermKind == LocalTermKind.TermLinkUsingPath); if (this.termLinkSourcePathPartLookups == null) { this.termLinkSourcePathPartLookups = new List <TermLinkSourcePathPartLookup>(); foreach (string partName in this.localTerm.GetTermLinkSourcePathParts()) { this.termLinkSourcePathPartLookups.Add(new TermLinkSourcePathPartLookup(partName)); } // See how far we can get by matching objects from the LocalTermStore LocalTaxonomyItem localParentItem = this.Controller.LocalTermStore; foreach (TermLinkSourcePathPartLookup lookup in this.termLinkSourcePathPartLookups) { LocalTaxonomyItem localItem = localParentItem.ChildItems .FirstOrDefault(x => x.Name == lookup.PartName); if (localItem != null) { lookup.LocalTaxonomyItem = localItem; Debug.Assert(lookup.ItemIsInLocalTermStore); lookup.Uploader = this.Controller.GetUploader(localItem); localParentItem = localItem; } } } // Make sure the local parents have been created/found foreach (TermLinkSourcePathPartLookup lookup in this.termLinkSourcePathPartLookups) { if (!lookup.ItemIsInLocalTermStore) { break; } if (!lookup.Uploader.FoundClientObject) { this.WaitForBlocker(lookup.Uploader); return(false); } lookup.ClientObject = lookup.Uploader.ClientTaxonomyItem; Debug.Assert(lookup.ClientObject != null); } if (this.termLinkSourcePathPartLookups.Any(x => !x.ItemIsInLocalTermStore)) { this.SetClientWorkingLanguageToDefault(); this.termLinkSourcePathExceptionHandlingScope = new ExceptionHandlingScope(this.ClientContext); using (this.termLinkSourcePathExceptionHandlingScope.StartScope()) { using (this.termLinkSourcePathExceptionHandlingScope.StartTry()) { // For anything else, we need to query it ourselves for (int i = 0; i < this.termLinkSourcePathPartLookups.Count; ++i) { TermLinkSourcePathPartLookup lookup = this.termLinkSourcePathPartLookups[i]; if (lookup.ItemIsInLocalTermStore) { continue; } // NOTE: For these queries we don't use CsomHelpers.FlushCachedProperties() // because the objects are outside the LocalTermStore, so we don't need to // worry about the cached copies getting invalidated by the uploaders. if (i == 0) { // Find a Group lookup.ClientObject = this.Controller.ClientTermStore.Groups.GetByName(lookup.PartName); } else if (i == 1) { // Find a TermSet TermGroup clientGroup = (TermGroup)this.termLinkSourcePathPartLookups[0].ClientObject; lookup.ClientObject = clientGroup.TermSets.GetByName(lookup.PartName); } else { // Find a term TermSetItem clientTermContainer = (TermSetItem)this.termLinkSourcePathPartLookups[i - 1].ClientObject; lookup.ClientObject = clientTermContainer.Terms.GetByName(lookup.PartName); } } } using (this.termLinkSourcePathExceptionHandlingScope.StartCatch()) { } } } this.clientTermOtherInstance = (Term)this.termLinkSourcePathPartLookups.Last().ClientObject; Debug.Assert(this.clientTermOtherInstance != null); } return(true); }
private void CreateTerm(ClientContext context, TermStore termStore, ShTerm shTerm, TermSetItem parentTerm) { var spTerm = termStore.GetTerm(shTerm.Id); context.Load(spTerm, t => t.Terms); context.ExecuteQuery(); if (spTerm.ServerObjectIsNull != null && spTerm.ServerObjectIsNull.Value) { spTerm = parentTerm.CreateTerm(shTerm.Title, termStore.DefaultLanguage, shTerm.Id); if (!string.IsNullOrEmpty(shTerm.CustomSortOrder)) spTerm.CustomSortOrder = shTerm.CustomSortOrder; spTerm.IsAvailableForTagging = !shTerm.NotAvailableForTagging; foreach(var prop in shTerm.CustomProperties) { spTerm.SetCustomProperty(prop.Key, prop.Value); } foreach (var prop in shTerm.LocalCustomProperties) { spTerm.SetLocalCustomProperty(prop.Key, prop.Value); } context.Load(spTerm); context.ExecuteQuery(); } foreach (ShTerm childTerm in shTerm.Terms) { CreateTerm(context, termStore, childTerm, spTerm); } }
private void AddTermsToConfig(ClientContext context, TermSetItem spTerm, ShTermSetItem shTermSetItem) { context.Load(spTerm, t => t.Terms.Include( item => item.Id, item => item.IsAvailableForTagging, item => item.IsDeprecated, item => item.IsReused, item => item.Description, item => item.Name, item => item.LocalCustomProperties, item => item.Labels)); context.ExecuteQuery(); foreach (var spChildTerm in spTerm.Terms) { var shTerm = new ShTerm(spChildTerm.Id, spChildTerm.Name); foreach (var label in spChildTerm.Labels) { shTerm.Labels.Add(new ShTermLabel { Language = label.Language, Value = label.Value, IsDefaultForLanguage = label.IsDefaultForLanguage }); } shTerm.LocalCustomProperties = spChildTerm.LocalCustomProperties; AddTermsToConfig(context, spChildTerm, shTerm); shTermSetItem.Terms.Add(shTerm); } }
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); } } }
private static void UpdateTerm(TermSetItem termSet, ManagedMetadataEntity entity, IEnumerable<IEnumerable<ManagedMetadataEntity>> termLevels, int currLevel) { int lcid = entity.LCID.HasValue ? entity.LCID.Value : 1033; string termName = TaxonomyItem.NormalizeName(entity.GetTermLevel(currLevel)); if (string.IsNullOrEmpty(termName)) { return; } Term term = termSet is TermSet ? (termSet as TermSet).GetTerms(termName, lcid, true, StringMatchOption.ExactMatch, 1, false). FirstOrDefault() : termSet.Terms.FirstOrDefault(t => t.Name.Equals(termName, StringComparison.InvariantCulture)); if (term == null) { //try //{ term = termSet.CreateTerm(termName, lcid, Guid.NewGuid()); term.IsAvailableForTagging = entity.AvailableForTagging; //} //catch (Exception ex) //{ //} } LabelCollection allLabels = term.GetAllLabels(lcid); if (allLabels.Count == 0 || !allLabels.Select(x => x.Value).Contains(termName)) { //try //{ term.CreateLabel(termName, lcid, true); term.SetDescription(entity.TermDescription, lcid); //} //catch (Exception ex) //{ //} } if (termLevels == null) { return; } if (currLevel >= termLevels.Count()) return; IEnumerable<ManagedMetadataEntity> childList = termLevels.ElementAt(currLevel).Where( t => t.HasSameLevelTerm(currLevel, entity)); foreach (ManagedMetadataEntity childEntity in childList) { UpdateTerm(term, childEntity, termLevels, currLevel + 1); } }
/// <summary> /// </summary> public static string MmdNormalize(object o) { return(TermSetItem.NormalizeName(GenUtil.SafeTrim(o))); }