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); } }
// 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); } }
public SPOTerm CreateTerm(string name, int lcid, Guid newTermId) { Term term = _termSetItem.CreateTerm(name, lcid, newTermId); _termSetItem.Context.ExecuteQuery(); _termSetItem.Context.Load(term); _termSetItem.Context.ExecuteQuery(); _terms = null; return(new SPOTerm(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); }
// 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 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); } }
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"); 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 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 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); // 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); } } }