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"); 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"); 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); } } }