/// <summary> /// Wires up MMS field to the specified term set. /// </summary> /// <param name="web">Site to be processed - can be root web or sub site</param> /// <param name="field">Field to be wired up</param> /// <param name="mmsGroupName">Taxonomy group</param> /// <param name="mmsTermSetName">Term set name</param> public static void WireUpTaxonomyField(this Web web, Field field, string mmsGroupName, string mmsTermSetName) { TermStore termStore = GetDefaultTermStore(web); if (termStore == null) { throw new NullReferenceException("The default term store is not available."); } if (string.IsNullOrEmpty(mmsTermSetName)) { throw new ArgumentNullException("mmsTermSetName", "The MMS term set is not specified."); } // get the term group and term set TermGroup termGroup = termStore.Groups.GetByName(mmsGroupName); TermSet termSet = termGroup.TermSets.GetByName(mmsTermSetName); web.Context.Load(termStore); web.Context.Load(termSet); web.Context.ExecuteQuery(); // set the SSP ID and Term Set ID on the taxonomy field var taxField = web.Context.CastTo <TaxonomyField>(field); taxField.SspId = termStore.Id; taxField.TermSetId = termSet.Id; taxField.Update(); web.Context.ExecuteQuery(); }
protected TermGroup FindGroup(TermStoreModelHost storeModelHost, TaxonomyTermGroupDefinition groupModel) { var termStore = storeModelHost.HostTermStore; var context = termStore.Context; TermGroup currentGroup = null; if (groupModel.IsSiteCollectionGroup) { currentGroup = FindSiteCollectionGroup(storeModelHost, groupModel); return(currentGroup); } if (groupModel.Id.HasValue) { var scope = new ExceptionHandlingScope(context); using (scope.StartScope()) { using (scope.StartTry()) { currentGroup = termStore.Groups.GetById(groupModel.Id.Value); context.Load(currentGroup); } using (scope.StartCatch()) { } } } else if (!string.IsNullOrEmpty(groupModel.Name)) { var scope = new ExceptionHandlingScope(context); using (scope.StartScope()) { using (scope.StartTry()) { currentGroup = termStore.Groups.GetByName(groupModel.Name); context.Load(currentGroup); } using (scope.StartCatch()) { } } } context.ExecuteQueryWithTrace(); if (currentGroup != null && currentGroup.ServerObjectIsNull == false) { context.Load(currentGroup, g => g.Id); context.Load(currentGroup, g => g.Name); context.ExecuteQueryWithTrace(); return(currentGroup); } return(null); }
public void MoveTermGroup(ClientContext sourceCtx, ClientContext targetCtx, string termGroup) { this._termStoreOperations = new List <TermOperation>(); this.sourceTermStore = this.GetTermStoreObject(sourceCtx); this.targetTermStore = this.GetTermStoreObject(targetCtx); this.sourceTermGroup = this.GetTermGroup(sourceCtx, sourceTermStore, termGroup); this.targetTermGroup = this.GetTermGroup(targetCtx, targetTermStore, termGroup); if (sourceTermGroup == null) { return; } if (targetTermGroup != null) { if (sourceTermGroup.Id != targetTermGroup.Id) { throw new Exception("The Term groups have different ID's. I don't know how to work it."); } } else { this.CreateTargetNewTermGroup(sourceCtx, targetCtx, sourceTermGroup, targetTermStore); } }
public void MoveTermGroup(ClientContext sourceCtx, ClientContext targetCtx, string termGroup) { this._termStoreOperations = new List<TermOperation>(); this.sourceTermStore = this.GetTermStoreObject(sourceCtx); this.targetTermStore = this.GetTermStoreObject(targetCtx); this.sourceTermGroup = this.GetTermGroup(sourceCtx, sourceTermStore, termGroup); this.targetTermGroup = this.GetTermGroup(targetCtx, targetTermStore, termGroup); if (sourceTermGroup == null) { return; } if (targetTermGroup != null) { if (sourceTermGroup.Id != targetTermGroup.Id) { throw new Exception("The Term groups have different ID's. I don't know how to work it."); } } else { this.CreateTargetNewTermGroup(sourceCtx, targetCtx, sourceTermGroup, targetTermStore); } }
//GetItems //GetTaxanomy public List <SelectListItem> GetTaxanomy(ClientContext context) { List <SelectListItem> items = new List <SelectListItem>(); TermCollection terms = null; TaxonomySession taxonomy = TaxonomySession.GetTaxonomySession(context); if (taxonomy != null) { TermStore termStore = taxonomy.GetDefaultKeywordsTermStore(); if (termStore != null) { TermGroup termGroup = termStore.Groups.GetByName(TERMGROUPNAME); TermSet termSet = termGroup.TermSets.GetByName(TERMSETNAME); terms = termSet.GetAllTerms(); context.Load(terms); context.ExecuteQuery(); foreach (var term in terms) { items.Add(new SelectListItem { Text = term.Name, Value = term.Id.ToString() }); } } } return(items); }
//ModifyItem //CreateTermSet public void CreateTermSet(ClientContext context) { //English(Default English Locale ID) int lcid = 1033; //Get termSession TaxonomySession taxonomy = TaxonomySession.GetTaxonomySession(context); if (taxonomy != null) { //Get TermStore TermStore termStore = taxonomy.GetDefaultKeywordsTermStore(); if (termStore != null) { //Set TermGroup TermGroup termGroup = termStore.CreateGroup(TERMGROUPNAME, Guid.NewGuid()); //Set TermSet TermSet termSetCollection = termGroup.CreateTermSet(TERMSETNAME, Guid.NewGuid(), lcid); //Set Terms termSetCollection.CreateTerm(TERM_ELEKTRONIK, lcid, Guid.NewGuid()); termSetCollection.CreateTerm(TERM_FORDON, lcid, Guid.NewGuid()); termSetCollection.CreateTerm(TERM_FRITIDOCHHOBBY, lcid, Guid.NewGuid()); termSetCollection.CreateTerm(TERM_HYGIENARTIKLAR, lcid, Guid.NewGuid()); termSetCollection.CreateTerm(TERM_MÖBLER, lcid, Guid.NewGuid()); context.ExecuteQuery(); } } }
private TermGroup GetTermGroup(ClientContext ctx, TermStore termStore, string groupName) { TermGroup _termGroup = termStore.Groups.GetByName(groupName); ctx.Load(_termGroup, group => group.Name, group => group.Id, group => group.Description, group => group.TermSets.Include( termSet => termSet.Name, termSet => termSet.Id)); try { ctx.ExecuteQuery(); if (!_termGroup.ServerObjectIsNull.Value) { return(_termGroup); } else { return(null); } } catch (Exception _ex) { return(null); } }
//gavdcodeend 16 //gavdcodebegin 14 static void SpCsPnpcoreCreateTermSetEnsure(ClientContext spCtx) { TermGroup myTermGroup = spCtx.Site.EnsureTermGroup("CsPnpcoreTermGroupEns"); TermSet myTermSet = myTermGroup.EnsureTermSet("CsPnpcoreTermSetEns"); Console.WriteLine(myTermSet.Id); }
private static void CreateNecessaryMMSTermsToCloud(ClientContext cc) { // Get access to taxonomy CSOM TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(cc); cc.Load(taxonomySession); cc.ExecuteQuery(); if (taxonomySession != null) { TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore(); if (termStore != null) { // // Create group, termset, and terms. // TermGroup myGroup = termStore.CreateGroup("Custom", Guid.NewGuid()); TermSet myTermSet = myGroup.CreateTermSet("Colors", Guid.NewGuid(), 1033); myTermSet.CreateTerm("Red", 1033, Guid.NewGuid()); myTermSet.CreateTerm("Orange", 1033, Guid.NewGuid()); myTermSet.CreateTerm("Yellow", 1033, Guid.NewGuid()); myTermSet.CreateTerm("Green", 1033, Guid.NewGuid()); myTermSet.CreateTerm("Blue", 1033, Guid.NewGuid()); myTermSet.CreateTerm("Purple", 1033, Guid.NewGuid()); cc.ExecuteQuery(); } } }
public void GetTermSetIds(TaxonomyTypes taxonomyType, string termSetName, string groupName, bool createIfNotExisting, out Guid termStoreId, out Guid termSetId) { TermStore termStore; TermSet termSet; Site site = _clientContext.Site; GetTermSet(taxonomyType, termSetName, groupName, createIfNotExisting, out termStore, out termSet); if (termSet == null) { if (!createIfNotExisting) { throw new KeyNotFoundException("Taxonomy missing"); } TermGroup termGroup = groupName == null ? termStore.GetSiteCollectionGroup(site, createIfNotExisting) : termStore.GetTermGroupByName(groupName); termSetId = Guid.NewGuid(); TermSet termSetColl = termGroup.CreateTermSet(termSetName, termSetId, Lcid); termSetColl.IsOpenForTermCreation = true; _clientContext.Load(termGroup.TermSets); _clientContext.ExecuteQuery(); termSet = termGroup.TermSets.FirstOrDefault(ts => ts.Name == termSetName); } _clientContext.Load(termStore, ts => ts.Id); _clientContext.ExecuteQuery(); termStoreId = termStore.Id; termSetId = termSet == null ? Guid.Empty : termSet.Id; }
/// <summary> /// Can be used to create taxonomy field remotely to web. Associated to group and term set in the GetDefaultSiteCollectionTermStore /// </summary> /// <param name="web">Site to be processed - can be root web or sub site</param> /// <param name="id">Unique Id for the taxonomy field</param> /// <param name="internalName">Internal Name of the field</param> /// <param name="displayName">Display name</param> /// <param name="group">Site column group</param> /// <param name="mmsGroupName">Taxonomy group </param> /// <param name="mmsTermSetName">Term set name</param> /// <param name="multiValue">If true, create a multi value field</param> /// <returns>New taxonomy field</returns> public static Field CreateTaxonomyField(this Web web, Guid id, string internalName, string displayName, string group, string mmsGroupName, string mmsTermSetName, bool multiValue = false) { id.ValidateNotNullOrEmpty("id"); internalName.ValidateNotNullOrEmpty("internalName"); displayName.ValidateNotNullOrEmpty("displayName"); // Group can be emtpy mmsGroupName.ValidateNotNullOrEmpty("mmsGroupName"); mmsTermSetName.ValidateNotNullOrEmpty("mmsTermSetName"); TermStore termStore = GetDefaultTermStore(web); if (termStore == null) { throw new NullReferenceException("The default term store is not available."); } // get the term group and term set TermGroup termGroup = termStore.Groups.GetByName(mmsGroupName); TermSet termSet = termGroup.TermSets.GetByName(mmsTermSetName); web.Context.Load(termStore); web.Context.Load(termSet); web.Context.ExecuteQuery(); return(web.CreateTaxonomyField(id, internalName, displayName, group, termSet, multiValue)); }
public static void CreateProductTaxonomy(ClientContext ctx) { TermStore store = ctx.Site.GetDefaultKeywordsTermStore(); TermGroup productCategoryGroup = store.GetTermGroupByName("OD2"); if (productCategoryGroup == null) { productCategoryGroup = store.CreateTermGroup("OD2", Constants.TAXONOMY_OD2_GROUP_ID.ToGuid(), "Stuff for od2"); } TermSet ts = store.GetTermSet(Constants.TAXONOMY_PRODUCTCAT_TERMSET_ID.ToGuid()); ctx.Load(ts); ctx.ExecuteQuery(); // if term store does not exist create a new one. if (ts.ServerObjectIsNull.Value) { ts = productCategoryGroup.CreateTermSet("Product Category", Constants.TAXONOMY_PRODUCTCAT_TERMSET_ID.ToGuid(), 1033); store.CommitAll(); ctx.Load(ts); ctx.ExecuteQuery(); ts.CreateTerm("Prod Cat 1", 1033, Guid.NewGuid()); ts.CreateTerm("Prod Cat 2", 1033, Guid.NewGuid()); ts.CreateTerm("Prod Cat 3", 1033, Guid.NewGuid()); ts.CreateTerm("Prod Cat 4", 1033, Guid.NewGuid()); store.CommitAll(); ctx.ExecuteQuery(); } }
public static List <ReferenceObject> GetDependenciesObjects(bool?returnOnlyMasterWorks, string guidStringForSearch) { Filter filter = new Filter(ProjectDependenciesReferenceInfo); AddTermByGuidParamPE(ref filter, SynchronizationParameterGuids.param_DependencyType_Guid, 5, (int)LogicalOperator.And); // Условие: в названии содержится "является укрупнением" //ReferenceObjectTerm termMasterWork = new ReferenceObjectTerm(LogicalOperator.Or); ReferenceObjectTerm termMasterWork = AddTermByGuidParamPE(ref filter, SynchronizationParameterGuids.param_MasterWork_Guid, guidStringForSearch); // Условие: в названии содержится "является детализацией" ReferenceObjectTerm termSlaveWork = AddTermByGuidParamPE(ref filter, SynchronizationParameterGuids.param_SlaveWork_Guid, guidStringForSearch, (int)LogicalOperator.Or); // Группируем условия в отдельную группу (другими словами добавляем скобки) TermGroup group1 = filter.Terms.GroupTerms(new Term[] { termMasterWork, termSlaveWork }); //редактируем при необходимости if (returnOnlyMasterWorks != null) { if (!(bool)returnOnlyMasterWorks) { group1.Remove(termSlaveWork); } else { group1.Remove(termMasterWork); } } return(ProjectDependenciesReference.Find(filter)); }
/// <summary> /// Can be used to create taxonomy field remotely in a list. Associated to group and term set in the GetDefaultSiteCollectionTermStore /// </summary> /// <param name="list">List to be processed</param> /// <param name="id">Unique Id for the taxonomy field</param> /// <param name="internalName">Internal Name of the field</param> /// <param name="displayName">Display name</param> /// <param name="group">Site column group</param> /// <param name="mmsGroupName">Taxonomy group </param> /// <param name="mmsTermSetName">Term set name</param> /// <param name="multiValue">If true, create multi value field</param> /// <returns>New taxonomy field</returns> public static Field CreateTaxonomyField(this List list, Guid id, string internalName, string displayName, string group, string mmsGroupName, string mmsTermSetName, bool multiValue = false) { id.ValidateNotNullOrEmpty("id"); internalName.ValidateNotNullOrEmpty("internalName"); displayName.ValidateNotNullOrEmpty("displayName"); mmsGroupName.ValidateNotNullOrEmpty("mmsGroupName"); mmsTermSetName.ValidateNotNullOrEmpty("mmsTermSetName"); var clientContext = list.Context as ClientContext; TermStore termStore = clientContext.Site.GetDefaultSiteCollectionTermStore(); if (termStore == null) { throw new NullReferenceException("The default term store is not available."); } // get the term group and term set TermGroup termGroup = termStore.Groups.GetByName(mmsGroupName); TermSet termSet = termGroup.TermSets.GetByName(mmsTermSetName); list.Context.Load(termStore); list.Context.Load(termSet); list.Context.ExecuteQuery(); return(list.CreateTaxonomyField(id, internalName, displayName, group, termSet, multiValue)); }
public static void CreateTaxonomy(ClientContext ctx) { TermStore store = ctx.Site.GetDefaultSiteCollectionTermStore(); TermGroup group = store.GetGroup("{0671B184-F909-4DCA-B383-6E05C969BEF1}".ToGuid()); ctx.Load(group); ctx.ExecuteQuery(); if (group.ServerObjectIsNull()) { group = store.CreateTermGroup("David1", "{0671B184-F909-4DCA-B383-6E05C969BEF1}".ToGuid()); } TermSet ts = group.EnsureTermSet("Color", "{19419441-6982-4978-8800-F12A46E6FE37}".ToGuid(), 1033); // Term term = ts.CreateTerm("Green", 1033, "{FA6628FA-9790-4622-9B54-23D103A67FCD}".ToGuid()); // Term term2 = ts.CreateTerm("Röd", 1053, "{24FA5756-25E8-4257-A4E5-D53C3D48B595}".ToGuid()); // ctx.ExecuteQuery(); // term2.EnsureLabel(1033, "Red", false); Term term2 = ts.CreateTerm("Blue", 1033, "{73E90F88-BD96-4066-8237-2D0DAAE3F11C}".ToGuid()); ctx.ExecuteQuery(); term2.EnsureLabel(1053, "Blå", false); }
/// <summary> /// Wires up MMS field to the specified term set. /// </summary> /// <param name="list">List to be processed</param> /// <param name="field">Field to be wired up</param> /// <param name="mmsGroupName">Taxonomy group</param> /// <param name="mmsTermSetName">Term set name</param> public static void WireUpTaxonomyField(this List list, Field field, string mmsGroupName, string mmsTermSetName, bool allowMultipleValues = false) { var clientContext = list.Context as ClientContext; TermStore termStore = clientContext.Site.GetDefaultSiteCollectionTermStore(); if (termStore == null) { throw new NullReferenceException("The default term store is not available."); } if (string.IsNullOrEmpty(mmsTermSetName)) { throw new ArgumentNullException("mmsTermSetName", "The MMS term set is not specified."); } // get the term group and term set TermGroup termGroup = termStore.Groups.GetByName(mmsGroupName); TermSet termSet = termGroup.TermSets.GetByName(mmsTermSetName); clientContext.Load(termStore); clientContext.Load(termSet); clientContext.ExecuteQuery(); list.WireUpTaxonomyField(field, termSet, allowMultipleValues); }
public void Import(TermGroup parentGroup) { if (parentGroup == null || parentGroup.ServerObjectIsNull.Value) { throw new ArgumentNullException("parentGroup", "The parent Group object is null."); } LoadWorkingLanguage(parentGroup.TermStore); if (_xml.DocumentElement.Name == "TermSets") { XmlNodeList termSetNodes = _xml.SelectNodes("./TermSets/TermSet"); if (termSetNodes == null || termSetNodes.Count == 0) { _cmdlet.WriteWarning("No Term Set elements were defined in the import file for the Group."); return; } foreach (XmlElement termSetElement in termSetNodes) { Import(termSetElement, parentGroup); } } else if (_xml.DocumentElement.Name == "TermSet") { Import(_xml.DocumentElement, parentGroup); } parentGroup.TermStore.CommitAll(); parentGroup.Context.ExecuteQuery(); SetManagedNavigationOn(_LocalNavTermSetId); }
/// <summary> /// Iterates through the taxonomy hierarchy for the specified term set group. /// </summary> /// <param name="clientContext">Client content for specified site</param> /// <param name="termStore">Term Store object</param> /// <param name="termStoreDetails">Term Store object containing Term store data</param> /// <returns>Fetch Group Terms Status</returns> private TaxonomyResponseVM GetTaxonomyHierarchy(TermStore termStore, TermStoreDetails termStoreDetails) { try { if (generalSettings.IsTenantDeployment) { foreach (TermGroup termGroup in termStore.Groups) { if (termGroup.Name == termStoreDetails.TermGroup) { taxonomyResponseVM = FetchGroupTerms(termGroup, termStoreDetails); break; } } } else { TermGroup termGroup = termStore.GetSiteCollectionGroup(clientContext.Site, false); taxonomyResponseVM = FetchGroupTerms(termGroup, termStoreDetails); } } catch (Exception ex) { customLogger.LogError(ex, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, logTables.SPOLogTable); throw; } return(taxonomyResponseVM); }
public SPOTermGroup CreateGroup(string name, Guid id, string description) { var ctx = _termStore.Context; TermGroup group = null; ExceptionHandlingScope scope = new ExceptionHandlingScope(ctx); using (scope.StartScope()) { using (scope.StartTry()) { group = _termStore.Groups.GetByName(name); ctx.Load(group); } using (scope.StartCatch()) { } } ctx.ExecuteQuery(); if (group == null || group.ServerObjectIsNull == null || group.ServerObjectIsNull.Value) { group = _termStore.CreateGroup(name, id); group.Description = description; ctx.ExecuteQuery(); ctx.Load(group); ctx.ExecuteQuery(); return(new SPOTermGroup(group)); } else { throw new Exception("The specified term group already exists."); } }
/// <summary> /// Create client terms /// </summary> /// <param name="TermsData">Data for client terms</param> /// <param name="clientContext">Client Context object</param> private static void CreateTermsForClients(List <CustomClientGroup> TermsData, ClientContext clientContext) { TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext); clientContext.Load(taxonomySession.TermStores); //Execute the query to the server clientContext.ExecuteQuery(); TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore(); clientContext.Load(termStore.Groups); //Execute the query to the server clientContext.ExecuteQuery(); foreach (CustomClientGroup cltGroup in TermsData) { TermGroup group = termStore.Groups.Where(termGroup => termGroup.Name == cltGroup.name).Count() > 0 ? termStore.Groups.GetByName(cltGroup.name) : termStore.CreateGroup(cltGroup.name, Guid.NewGuid()); if (null != group) { TermSet cltTermSet = group.CreateTermSet(ConfigurationManager.AppSettings["clientterm"], Guid.NewGuid(), 1033); TermSet cltIDTermSet = group.CreateTermSet(ConfigurationManager.AppSettings["clientidterm"], Guid.NewGuid(), 1033); foreach (ClientList clients in cltGroup.clt) { Term clientCustom = cltTermSet.CreateTerm(clients.ClientName, 1033, Guid.NewGuid()); cltIDTermSet.CreateTerm(clients.ClientID, 1033, Guid.NewGuid()); clientCustom.SetCustomProperty("ClientID", clients.ClientID); clientCustom.SetCustomProperty("ClientURL", clients.ClientURL); } } } termStore.CommitAll(); clientContext.Load(termStore); //Execute the query to the server clientContext.ExecuteQuery(); }
public static void CreateTaxonomyFavColor(ClientContext ctx) { TermStore store = ctx.Site.GetDefaultSiteCollectionTermStore(); TermGroup group = store.GetTermGroupByName("School"); if (group == null) { group = store.CreateTermGroup("School", "{13E6924A-196D-4313-A1B4-3A21AF3958CF}".ToGuid(), "description"); } TermSet termSet = group.EnsureTermSet("Fav Color", "{3D4C7DE0-3867-44C3-871A-C36DEC4E1970}".ToGuid(), 1033); Term term = store.GetTerm("{1BB08D14-4464-410C-A27C-BA2EB8EFE9DE}".ToGuid()); ctx.Load(term); ctx.ExecuteQuery(); if (term.ServerObjectIsNull()) { termSet.CreateTerm("Green", 1033, "{1BB08D14-4464-410C-A27C-BA2EB8EFE9DE}".ToGuid()); termSet.CreateTerm("Blue", 1033, "{E96EF7E0-7F9B-4F66-BA1A-C2DBCC73DD78}".ToGuid()); termSet.CreateTerm("Red", 1033, "{13E22744-483D-49D2-B92D-6BD2E0C10D9C}".ToGuid()); ctx.ExecuteQuery(); } }
private static string GetValueFromTermSet(TermGroup group, string[] termNames, TraceWriter log, ClientContext context) { string value = string.Empty; foreach (TermSet termSet in group.TermSets) { if (termSet.Name == termNames[1]) { log.Info($"Found {termNames[1]} term set"); foreach (Term term in termSet.Terms) { if (term.Name == termNames[2]) { context.Load(term.Terms, t => t.Include( st => st.Name, st => st.LocalCustomProperties)); context.ExecuteQueryRetry(); log.Info($"Found {term.Name} term"); value = term.LocalCustomProperties["Value"]; log.Info($" {termNames[2]} is {value}"); } } } } return(value); }
public static void CreateIssueCategoryTermSet(ClientContext ctx) { //TaxonomySession taxSession = TaxonomySession.GetTaxonomySession(ctx); //TermStore store = taxSession.TermStores[0]; TermStore store = ctx.Site.GetDefaultSiteCollectionTermStore(); TermGroup group = store.GetTermGroupByName("OD1Test"); if (group == null) { group = store.CreateTermGroup("OD1Test", "{13E6924A-196D-4313-A1B4-3A21AF3958CF}".ToGuid(), "description"); } TermSet termSet = group.EnsureTermSet("Issue Category", "{3D4C7DE0-3867-44C3-871A-C36DEC4E1970}".ToGuid(), 1033); Term term = store.GetTerm("{1BB08D14-4464-410C-A27C-BA2EB8EFE9DE}".ToGuid()); ctx.Load(term); ctx.ExecuteQuery(); if (term == null) { termSet.CreateTerm("Just Right", 1033, "{1BB08D14-4464-410C-A27C-BA2EB8EFE9DE}".ToGuid()); termSet.CreateTerm("Low Priority", 1033, "{E96EF7E0-7F9B-4F66-BA1A-C2DBCC73DD78}".ToGuid()); termSet.CreateTerm("Urgent", 1033, "{13E22744-483D-49D2-B92D-6BD2E0C10D9C}".ToGuid()); ctx.ExecuteQuery(); } }
protected TermSet FindTermSet(TermGroup termGroup, TaxonomyTermSetDefinition termSetModel) { TermSet result = null; var context = termGroup.Context; context.Load(termGroup.TermSets); context.ExecuteQueryWithTrace(); if (termSetModel.Id.HasValue) { var scope = new ExceptionHandlingScope(context); using (scope.StartScope()) { using (scope.StartTry()) { result = termGroup.TermSets.GetById(termSetModel.Id.Value); context.Load(result); } using (scope.StartCatch()) { } } } else if (!string.IsNullOrEmpty(termSetModel.Name)) { var scope = new ExceptionHandlingScope(context); using (scope.StartScope()) { using (scope.StartTry()) { result = termGroup.TermSets.GetByName(termSetModel.Name); context.Load(result); } using (scope.StartCatch()) { } } } context.ExecuteQueryWithTrace(); if (result != null && result.ServerObjectIsNull.HasValue && result.ServerObjectIsNull == false) { context.Load(result); //context.Load(result, g => g.Id); //context.Load(result, g => g.Name); context.ExecuteQueryWithTrace(); return(result); } return(null); }
protected TermSet FindTermSet(TermGroup termGroup, TaxonomyTermSetDefinition termSetModel) { TermSet result = null; var context = termGroup.Context; context.Load(termGroup.TermSets); context.ExecuteQueryWithTrace(); if (termSetModel.Id.HasValue) { var scope = new ExceptionHandlingScope(context); using (scope.StartScope()) { using (scope.StartTry()) { result = termGroup.TermSets.GetById(termSetModel.Id.Value); context.Load(result); } using (scope.StartCatch()) { } } } else if (!string.IsNullOrEmpty(termSetModel.Name)) { var scope = new ExceptionHandlingScope(context); using (scope.StartScope()) { using (scope.StartTry()) { result = termGroup.TermSets.GetByName(termSetModel.Name); context.Load(result); } using (scope.StartCatch()) { } } } context.ExecuteQueryWithTrace(); if (result != null && result.ServerObjectIsNull == false) { context.Load(result); //context.Load(result, g => g.Id); //context.Load(result, g => g.Name); context.ExecuteQueryWithTrace(); return result; } return null; }
/// <summary> /// Fetches the term store data for the term group specified /// </summary> /// <param name="clientContext">Client Context</param> /// <param name="termGroup">Term Group from the Term store</param> /// <param name="termStoreDetails">Details of the Term Group</param> /// <param name="returnFlagStatus">hierarchy of the Term Group</param> /// <returns>Hierarchy of the Term Group</returns> private TaxonomyResponseVM FetchGroupTerms(TermGroup termGroup, TermStoreDetails termStoreDetails) { try { //As a first step, load Practice Group Term set and anf its child terms only and not its child child terms clientContext.Load( termGroup, group => group.Name, group => group.TermSets.Include( termSet => termSet.Name, termSet => termSet.Id, termSet => termSet.Terms.Include( term => term.Name, term => term.Id, term => term.TermsCount, term => term.CustomProperties ))); clientContext.ExecuteQuery(); //This loop of the term set will get all the client termset and practice group term sets foreach (TermSet termSet in termGroup.TermSets) { if (termSet.Name == termStoreDetails.TermSetName) { //This condition is if the UI is requesting practice group terms if (termStoreDetails.TermSetName == taxonomySettings.PracticeGroupTermSetName) { taxonomyResponseVM.TermSets = GetManagedTermSetHierarchy(clientContext, termSet, termStoreDetails); } //This condition is if the UI is requesting client terms and the clients are defined as term set and not terms else if (termStoreDetails.TermSetName == taxonomySettings.ClientTermSetName) { taxonomyResponseVM.ClientTermSets = GetClientTermSetHierarchy(clientContext, termSet, termStoreDetails); } } } //if (termStoreDetails.TermSetName == taxonomySettings.ClientTermSetName && // taxonomySettings.ClientTermPath != ServiceConstants.CLIENT_TERM_PATH) //{ // //This condition is if the UI is requesting client terms and the clients are defined as terms and not term sets // foreach (TermSet termSet in termGroup.TermSets) // { // if (termSet.Name == taxonomySettings.ClientTermPath) // { // taxonomyResponseVM.ClientTermSets = GetClientTermSetHierarchy(clientContext, termSet, termStoreDetails); // } // } //} } catch (Exception ex) { customLogger.LogError(ex, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, logTables.SPOLogTable); throw; } return(taxonomyResponseVM); }
public TermGroupPipeBind(TermGroup termGroup) { if (!termGroup.IsPropertyAvailable("Id")) { termGroup.EnsureProperty(t => t.Id); } _id = termGroup.Id; }
public SPOTermGroup(TermGroup termGroup) : base(termGroup) { _termGroup = termGroup; if (_termGroup.IsPropertyAvailable("Description")) SetProp("Description", _termGroup.Description, false); }
//gavdcodeend 11 //gavdcodebegin 12 static void SpCsPnpcoreCreateTermGroup(ClientContext spCtx) { string termStoreName = "Taxonomy_hVIOdhme2obc+5zqZXqqUQ=="; TaxonomySession myTaxSession = TaxonomySession.GetTaxonomySession(spCtx); TermStore myTermStore = myTaxSession.TermStores.GetByName(termStoreName); TermGroup myTermGroup = myTermStore.CreateTermGroup("CsPnpcoreTermGroup"); }
public ActionResult BuyAuctions() { var model = new SearchAdvertisementViewModel(); model.FilterList.Add(new SelectListItem { Value = "PriceAsc", Text = "Pris (Stigande)" }); model.FilterList.Add(new SelectListItem { Value = "PriceDesc", Text = "Pris (Fallande)" }); model.FilterList.Add(new SelectListItem { Value = "DateAsc", Text = "Datum (Stigande)" }); model.FilterList.Add(new SelectListItem { Value = "DateDesc", Text = "Datum (Fallande)" }); SharePointContext spContext = Session["SpContext"] as SharePointContext; using (var clientContext = spContext.CreateUserClientContextForSPHost()) { TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext); if (taxonomySession != null) { TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore(); TermGroupCollection termGroupCol = termStore.Groups; clientContext.Load(termGroupCol, t => t.Where(y => y.Name == "Advertisements")); clientContext.ExecuteQuery(); TermGroup termGroup = termGroupCol.FirstOrDefault(); if (termGroup != null) { TermSet termSet = termGroup.TermSets.GetByName("Categories"); TermCollection terms = termSet.GetAllTerms(); clientContext.Load(termSet); clientContext.Load(terms); clientContext.ExecuteQuery(); foreach (Term term in terms) { SelectListItem newItem = new SelectListItem { Value = term.Name, Text = term.Name }; model.CategoryList.Add(newItem); } } } } model.CategoryList.OrderBy(x => x.Text); model.CategoryList.Insert(0, new SelectListItem { Value = "Alla", Text = "Alla" }); return(View(model)); }
public SPOTermGroup(TermGroup termGroup) : base(termGroup) { _termGroup = termGroup; if (_termGroup.IsPropertyAvailable("Description")) { SetProp("Description", _termGroup.Description, false); } }
public static void CreateProductCategoriesTermset() { string termSetName = "Product Categories"; var aspnetHttpContext = System.Web.HttpContext.Current; var spContext = SharePointContextProvider.Current.GetSharePointContext(aspnetHttpContext); using (var clientContext = spContext.CreateUserClientContextForSPHost()) { if (clientContext != null) { Site siteCollection = clientContext.Site; clientContext.Load(siteCollection, sc => sc.Url); clientContext.ExecuteQuery(); TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext); taxonomySession.UpdateCache(); clientContext.Load(taxonomySession, ts => ts.TermStores); clientContext.ExecuteQuery(); TermStore termStore = taxonomySession.TermStores.FirstOrDefault <TermStore>(); Guid localTermStoreID = termStore.Id; TermGroup group = termStore.GetSiteCollectionGroup(siteCollection, true); clientContext.Load(group); clientContext.Load(group.TermSets); clientContext.ExecuteQuery(); // make sure it's deleted if exists foreach (TermSet termset in group.TermSets) { if (termset.Name.Equals(termSetName)) { termset.DeleteObject(); termStore.CommitAll(); clientContext.ExecuteQuery(); } } Guid termSetId = Guid.NewGuid(); TermSet tset = group.CreateTermSet(termSetName, termSetId, 1033); termStore.CommitAll(); clientContext.ExecuteQuery(); foreach (var term in terms) { CreateTopLevelTerm(tset, term); } termStore.CommitAll(); clientContext.ExecuteQuery(); clientContext.ExecuteQuery(); termStore.CommitAll(); } } }
public static SiteCollectionModel GetSiteCollectionModel(string url) { SiteCollectionModel model = new SiteCollectionModel(); var aspnetHttpContext = System.Web.HttpContext.Current; var spContext = SharePointContextProvider.Current.GetSharePointContext(aspnetHttpContext); using (var clientContext = spContext.CreateUserClientContextForSPHost()) { if (clientContext != null) { Site siteCollection = clientContext.Site; clientContext.Load(siteCollection, sc => sc.Url); clientContext.ExecuteQuery(); model.Url = siteCollection.Url; TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext); taxonomySession.UpdateCache(); clientContext.Load(taxonomySession, ts => ts.TermStores); clientContext.ExecuteQuery(); TermStore termStore = taxonomySession.TermStores.FirstOrDefault <TermStore>(); Guid localTermStoreID = termStore.Id; TermGroup group = termStore.GetSiteCollectionGroup(siteCollection, false); clientContext.ExecuteQuery(); bool siteCollectionHasPrivateGroup = CSOMObjectExists(group); model.hasPrivateGroup = siteCollectionHasPrivateGroup; model.Termsets = new Dictionary <Guid, TermsetModel>(); if (siteCollectionHasPrivateGroup) { clientContext.Load(group, g => g.Id, g => g.Name, g => g.TermSets); clientContext.ExecuteQuery(); model.GroupId = group.Id; model.GroupName = group.Name; foreach (var ts in group.TermSets) { TermsetModel termset = new TermsetModel { TermsetId = ts.Id, DisplayName = ts.Name }; clientContext.Load(ts.Terms); clientContext.ExecuteQuery(); List <TermModel> terms = new List <TermModel>(); foreach (Term term in ts.Terms) { TermModel newTerm = new TermModel { TermName = term.Name, TermId = term.Id }; LoadChildTerms(term, newTerm, clientContext); terms.Add(newTerm); } termset.TopLevelTerms = terms; model.Termsets.Add(ts.Id, termset); } } } return(model); } }
private static TermSet AddTermSet(ClientContext clientContext, TermStore termStore, TermGroup termGroup) { var termSetId = new Guid("56ca0eea-635e-4cc1-ac35-fc2040f4cfe5"); var termSet = termStore.GetTermSet(termSetId); clientContext.Load(termSet, ts => ts.Terms); clientContext.ExecuteQuery(); if (termSet.ServerObjectIsNull.Value) { termSet = termGroup.CreateTermSet("Taxonomy Navigation", termSetId, 1033); termSet.SetCustomProperty("_Sys_Nav_IsNavigationTermSet", "True"); clientContext.Load(termSet, ts => ts.Terms); clientContext.ExecuteQuery(); } return termSet; }
public XmlDocument Export(TermGroup group) { if (group == null) throw new ArgumentNullException("group", "The Group object is null."); LoadLanguages(group.TermStore); XmlElement groupElement = AddGroupElement(null, group); var termSets = group.TermSets; if (!group.IsObjectPropertyInstantiated("TermSets") || !group.TermSets.AreItemsAvailable) { _ctx.Load(termSets); _ctx.ExecuteQuery(); } foreach (TermSet termSet in termSets) { Export(groupElement, termSet); } return _xml; }
static TermSet CreateTermset(ClientContext clientContext, TermGroup termGroup, string termSetName) { // delete termset if it already exists foreach (TermSet termset in termGroup.TermSets) { if (termset.Name.Equals(termSetName)) { termset.DeleteObject(); termGroup.TermStore.CommitAll(); clientContext.ExecuteQuery(); } } Guid termSetId = Guid.NewGuid(); TermSet newTermSet = termGroup.CreateTermSet(termSetName, termSetId, 1033); newTermSet.IsOpenForTermCreation = true; termGroup.TermStore.CommitAll(); clientContext.Load(newTermSet); clientContext.ExecuteQuery(); return newTermSet; }
/// <summary> /// Fetches the term store data for the term group specified /// </summary> /// <param name="clientContext">Client Context</param> /// <param name="termGroup">Term Group from the Term store</param> /// <param name="termStoreDetails">Details of the Term Group</param> /// <param name="returnFlagStatus">hierarchy of the Term Group</param> /// <returns>Hierarchy of the Term Group</returns> internal static string FetchGroupTerms(ClientContext clientContext, TermGroup termGroup, TermStoreDetails termStoreDetails, string returnFlagStatus) { clientContext.Load( termGroup, group => group.Name, group => group.TermSets.Include( termSet => termSet.Name, termSet => termSet.Id, termSet => termSet.Terms.Include( term => term.Name, term => term.Id, term => term.CustomProperties, term => term.Terms.Include( termArea => termArea.Name, termArea => termArea.Id, termArea => termArea.CustomProperties, termArea => termArea.Terms.Include( termSubArea => termSubArea.Name, termSubArea => termSubArea.Id, termSubArea => termSubArea.CustomProperties))))); clientContext.ExecuteQuery(); foreach (TermSet termSet in termGroup.TermSets) { if (termSet.Name == termStoreDetails.TermSetName) { if (termStoreDetails.TermSetName == ServiceConstantStrings.PracticeGroupTermSetName) { returnFlagStatus = GetPracticeGroupTermSetHierarchy(termSet, termStoreDetails); } else if (termStoreDetails.TermSetName == ServiceConstantStrings.ClientTermSetName) { returnFlagStatus = GetClientTermSetHierarchy(termSet, termStoreDetails); } } } return returnFlagStatus; }
/// <summary> /// Initializing Client List By Loading TermGroup /// </summary> /// <param name="clientContext">Client Context</param> /// <param name="termGroup">Term Group</param> /// <param name="termSetName">Term Store</param> /// <returns>Client List</returns> public static List<Client> IntializeClientObject(ClientContext clientContext, TermGroup termGroup, string termSetName) { Client client; List<Client> clients = new List<Client>(); TermSet termSet = termGroup.TermSets.GetByName(termSetName); TermCollection termColl = termSet.Terms; clientContext.Load(termColl); // Execute the query to the server clientContext.ExecuteQuery(); foreach (Term term in termColl) { client = new Client(); client.Name = term.Name; foreach (KeyValuePair<string, string> termProperty in term.CustomProperties) { if (termProperty.Key.Equals(Constants.URLDictKey)) { client.Url = termProperty.Value; } } clients.Add(client); } return clients; }
public void ProcessChanges(ClientContext sourceClientContext, ClientContext targetClientContext , List<string> termSetNames) { List<TermOperation> _list = new List<TermOperation>(); DateTime _startFrom = DateTime.Now.AddYears(-1); Console.WriteLine("Opening the taxonomy session"); TaxonomySession _sourceTaxonomySession = TaxonomySession.GetTaxonomySession(sourceClientContext); TermStore sourceTermStore = _sourceTaxonomySession.GetDefaultKeywordsTermStore(); sourceClientContext.Load(sourceTermStore); sourceClientContext.ExecuteQuery(); Console.WriteLine("Reading the changes"); ChangeInformation _ci = new ChangeInformation(sourceClientContext); _ci.StartTime = _startFrom; ChangedItemCollection _cic = sourceTermStore.GetChanges(_ci); sourceClientContext.Load(_cic); sourceClientContext.ExecuteQuery(); if (_cic.Count > 0) { bool noError = true; // Load up the taxonomy item names. TaxonomySession targetTaxonomySession = TaxonomySession.GetTaxonomySession(targetClientContext); TermStore targetTermStore = targetTaxonomySession.GetDefaultKeywordsTermStore(); targetClientContext.Load(targetTermStore, store => store.Name, store => store.DefaultLanguage, store => store.Groups.Include( group => group.Name, group => group.Id)); targetClientContext.ExecuteQuery(); foreach (ChangedItem _changeItem in _cic) { Guid g = _changeItem.Id; if (_changeItem.ItemType == ChangedItemType.Group) { TermGroup sourceTermGroup = sourceTermStore.GetGroup(_changeItem.Id); sourceClientContext.Load(sourceTermGroup, group => group.Name, group => group.Id, group => group.IsSystemGroup, group => group.Description); sourceClientContext.ExecuteQuery(); if (sourceTermGroup.ServerObjectIsNull.Value) { continue; } else { if (sourceTermGroup.IsSystemGroup) { Console.WriteLine("Group \"" + sourceTermGroup.Name + "\" is a system group"); continue; } } if (_changeItem.Operation == ChangedOperationType.DeleteObject) { TermGroup targetTermGroup = targetTermStore.GetGroup(_changeItem.Id); targetClientContext.Load(targetTermGroup, group => group.Name, group => group.Id, group => group.TermSets.Include( termSet => termSet.Name, termSet => termSet.Id)); targetClientContext.ExecuteQuery(); foreach (TermSet ts in targetTermGroup.TermSets) { Console.WriteLine("Deleting termset: " + ts.Name); TermOperation op = new TermOperation(); op.Term = ts.Name; op.Id = ts.Id.ToString(); op.Operation = "Delete"; op.Type = "TermSet"; _list.Add(op); ts.DeleteObject(); } Console.WriteLine("Deleting group: " + sourceTermGroup.Name); targetTermGroup.DeleteObject(); TermOperation op2 = new TermOperation(); op2.Term = sourceTermGroup.Name; op2.Id = _changeItem.Id.ToString(); op2.Operation = "Delete"; op2.Type = "TermGroup"; _list.Add(op2); targetClientContext.ExecuteQuery(); } else if (_changeItem.Operation == ChangedOperationType.Add) { TermGroup targetTermGroup = targetTermStore.GetGroup(_changeItem.Id); targetClientContext.Load(targetTermGroup, group => group.Name, group => group.Id, group => group.TermSets.Include( termSet => termSet.Name, termSet => termSet.Id)); targetClientContext.ExecuteQuery(); if (targetTermGroup.ServerObjectIsNull.Value) { TermGroup targetTermGroupTest = targetTermStore.Groups.GetByName(sourceTermGroup.Name); targetClientContext.Load(targetTermGroupTest, group => group.Name); try { targetClientContext.ExecuteQuery(); if (!targetTermGroupTest.ServerObjectIsNull.Value) { if (sourceTermGroup.Name.ToLower() == "system" || sourceTermGroup.Name.ToLower() == "people") { Console.WriteLine("Group: " + sourceTermGroup.Name + " already exists"); continue; } else { InvalidOperationException uEx = new InvalidOperationException("A group named: \"" + sourceTermGroup.Name + "\" already exists but with a different ID. Please delete the term group from the target termstore"); break; } } } catch { } Console.WriteLine("Adding group: " + sourceTermGroup.Name); TermGroup _targetTermGroup = targetTermStore.CreateGroup(sourceTermGroup.Name, _changeItem.Id); if(!string.IsNullOrEmpty(sourceTermGroup.Description)) { _targetTermGroup.Description = sourceTermGroup.Description; } targetClientContext.ExecuteQuery(); targetTermStore.CommitAll(); targetTermStore.RefreshLoad(); TermOperation op = new TermOperation(); op.Term = sourceTermGroup.Name; op.Id = _changeItem.Id.ToString(); op.Operation = "Add"; op.Type = "TermGroup"; _list.Add(op); } } else if (_changeItem.Operation == ChangedOperationType.Edit) { TermGroup targetTermGroup = targetTermStore.GetGroup(_changeItem.Id); targetClientContext.Load(targetTermGroup, group => group.Name, group => group.Id, group => group.TermSets.Include( termSet => termSet.Name, termSet => termSet.Id)); targetClientContext.ExecuteQuery(); if (targetTermGroup.ServerObjectIsNull.Value) { targetTermGroup = targetTermStore.Groups.GetByName(sourceTermGroup.Name); targetClientContext.Load(targetTermGroup, group => group.Name); targetClientContext.ExecuteQuery(); if (targetTermGroup.ServerObjectIsNull.Value) { noError = false; break; } } if (targetTermGroup.Name != sourceTermGroup.Name) { Console.WriteLine("Modifying group: " + sourceTermGroup.Name); targetTermGroup.Name = sourceTermGroup.Name; TermOperation op = new TermOperation(); op.Term = sourceTermGroup.Name; op.Id = _changeItem.Id.ToString(); op.Operation = "Modify"; op.Type = "TermGroup"; _list.Add(op); targetClientContext.ExecuteQuery(); } } } if (_changeItem.ItemType == ChangedItemType.TermSet) { TermSet sourceTermset = sourceTermStore.GetTermSet(_changeItem.Id); sourceClientContext.Load(sourceTermset, termset => termset.Name, termset => termset.Id, termset => termset.Description, termset => termset.Contact, termset => termset.CustomProperties, termset => termset.Group, group => group.Id); sourceClientContext.ExecuteQuery(); if (sourceTermset.ServerObjectIsNull.Value) { continue; } if (!termSetNames.Contains(sourceTermset.Name)) { continue; } if (_changeItem.Operation == ChangedOperationType.DeleteObject) { TermSet targetTermset = targetTermStore.GetTermSet(_changeItem.Id); targetClientContext.Load(targetTermset); targetClientContext.ExecuteQuery(); Console.WriteLine("Deleting termset: " + targetTermset.Name); targetTermset.DeleteObject(); targetClientContext.ExecuteQuery(); TermOperation op = new TermOperation(); op.Term = targetTermset.Name; op.Id = _changeItem.Id.ToString(); op.Operation = "Delete"; op.Type = "TermSet"; _list.Add(op); } else if (_changeItem.Operation == ChangedOperationType.Add) { TermGroup targetTermGroup = targetTermStore.GetGroup(sourceTermset.Group.Id); targetClientContext.Load(targetTermGroup, group => group.Name, group => group.IsSystemGroup, group => group.TermSets.Include( termSet => termSet.Name, termSet => termSet.Id)); targetClientContext.ExecuteQuery(); if (targetTermGroup.ServerObjectIsNull.Value) { //Group may exist with another name targetTermGroup = targetTermStore.Groups.GetByName(sourceTermset.Group.Name); targetClientContext.Load(targetTermGroup, group => group.Name, group => group.IsSystemGroup, group => group.TermSets.Include( termSet => termSet.Name, termSet => termSet.Id)); targetClientContext.ExecuteQuery(); if (targetTermGroup.ServerObjectIsNull.Value) { noError = false; break; } } TermSet targetTermSetCheck = targetTermGroup.TermSets.GetByName(sourceTermset.Name); targetClientContext.Load(targetTermSetCheck); try { targetClientContext.ExecuteQuery(); if (!targetTermSetCheck.ServerObjectIsNull.Value) { Console.WriteLine("Termset: " + sourceTermset.Name + " already exists"); continue; } } catch { } Console.WriteLine("Adding termset: " + sourceTermset.Name); targetTermGroup.CreateTermSet(sourceTermset.Name, _changeItem.Id, targetTermStore.DefaultLanguage); TermOperation op = new TermOperation(); op.Term = sourceTermset.Name; op.Id = _changeItem.Id.ToString(); op.Operation = "Add"; op.Type = "TermSet"; targetClientContext.ExecuteQuery(); targetTermStore.CommitAll(); targetTermStore.RefreshLoad(); _list.Add(op); } else if (_changeItem.Operation == ChangedOperationType.Edit) { TermGroup targetTermGroup = null; TermSet sourceTermSet = sourceTermStore.GetTermSet(_changeItem.Id); sourceClientContext.Load(sourceTermSet, termset => termset.Name); sourceClientContext.ExecuteQuery(); TermSet targetTermSet = targetTermStore.GetTermSet(_changeItem.Id); targetClientContext.Load(targetTermSet, termset => termset.Name); targetClientContext.ExecuteQuery(); if (targetTermSet.ServerObjectIsNull.Value) { targetTermGroup = targetTermStore.GetGroup(sourceTermset.Group.Id); targetClientContext.Load(targetTermGroup, group => group.Name, group => group.IsSystemGroup); targetClientContext.ExecuteQuery(); if (!targetTermGroup.ServerObjectIsNull.Value) { targetTermSet = targetTermGroup.TermSets.GetByName(sourceTermSet.Name); targetClientContext.Load(targetTermSet, termset => termset.Name); targetClientContext.ExecuteQuery(); } } if (!targetTermSet.ServerObjectIsNull.Value) { if (targetTermSet.Name != sourceTermSet.Name) { Console.WriteLine("Modifying termset: " + sourceTermSet.Name); targetTermSet.Name = sourceTermSet.Name; TermOperation op = new TermOperation(); op.Term = sourceTermSet.Name; op.Id = _changeItem.Id.ToString(); op.Operation = "Modify"; op.Type = "TermSet"; _list.Add(op); } } else { Console.WriteLine("Termset: " + sourceTermset.Name + " not found, creating it"); targetTermGroup.CreateTermSet(sourceTermset.Name, _changeItem.Id, targetTermStore.DefaultLanguage); TermOperation op = new TermOperation(); op.Term = sourceTermset.Name; op.Id = _changeItem.Id.ToString(); op.Operation = "Add"; op.Type = "TermSet"; _list.Add(op); } } } if (_changeItem.ItemType == ChangedItemType.Term) { Term sourceTerm = sourceTermStore.GetTerm(_changeItem.Id); sourceClientContext.Load(sourceTerm, term => term.Name, term => term.Description, term => term.Id, term => term.TermSet, termset => termset.Id); sourceClientContext.ExecuteQuery(); if (!sourceTerm.ServerObjectIsNull.Value) { TermSet sourceTermSet = sourceTermStore.GetTermSet(sourceTerm.TermSet.Id); sourceClientContext.Load(sourceTermSet, termset => termset.Name, termset => termset.Id, termset => termset.Group); sourceClientContext.ExecuteQuery(); if (!sourceTermSet.ServerObjectIsNull.Value) { if(!termSetNames.Contains(sourceTermSet.Name)) { continue; } } TermSet targetTermSet = targetTermStore.GetTermSet(sourceTerm.TermSet.Id); targetClientContext.Load(targetTermSet, termset => termset.Name); targetClientContext.ExecuteQuery(); if (targetTermSet.ServerObjectIsNull.Value) { noError = false; break; } if (_changeItem.Operation == ChangedOperationType.DeleteObject) { Term targetTerm = targetTermStore.GetTerm(_changeItem.Id); targetClientContext.Load(targetTerm); targetClientContext.ExecuteQuery(); Console.WriteLine("Deleting term: " + sourceTerm.Name); targetTerm.DeleteObject(); TermOperation op = new TermOperation(); op.Term = sourceTerm.Name; op.Id = _changeItem.Id.ToString(); op.Operation = "Delete"; op.Type = "Term"; _list.Add(op); } else if (_changeItem.Operation == ChangedOperationType.Add) { Term targetTerm = targetTermStore.GetTerm(sourceTerm.Id); targetClientContext.Load(targetTerm); targetClientContext.ExecuteQuery(); if (targetTerm.ServerObjectIsNull.Value) { Console.WriteLine("Creating term: " + sourceTerm.Name); Term _targetTerm = targetTermSet.CreateTerm(sourceTerm.Name, targetTermStore.DefaultLanguage, _changeItem.Id); if (!string.IsNullOrEmpty(sourceTerm.Description)) { _targetTerm.SetDescription(sourceTerm.Description, targetTermStore.DefaultLanguage); } //GetLabels from Source targetClientContext.ExecuteQuery(); TermOperation op = new TermOperation(); op.Term = sourceTerm.Name; op.Id = _changeItem.Id.ToString(); op.Operation = "Add"; op.Type = "Term"; _list.Add(op); } } else if (_changeItem.Operation == ChangedOperationType.Edit) { Term targetTerm = targetTermStore.GetTerm(_changeItem.Id); targetClientContext.Load(targetTerm, term => term.Name); targetClientContext.ExecuteQuery(); if (!targetTerm.ServerObjectIsNull.Value) { if (targetTerm.Name != sourceTerm.Name) { targetTerm.Name = sourceTerm.Name; TermOperation op = new TermOperation(); op.Term = sourceTerm.Name; op.Id = _changeItem.Id.ToString(); op.Operation = "Modify"; op.Type = "Term"; _list.Add(op); } } else { try { Term _targetTerm = targetTermSet.CreateTerm(sourceTerm.Name, targetTermStore.DefaultLanguage, _changeItem.Id); if (!string.IsNullOrEmpty(sourceTerm.Description)) { _targetTerm.SetDescription(sourceTerm.Description, targetTermStore.DefaultLanguage); } targetClientContext.ExecuteQuery(); Console.WriteLine("Term: " + sourceTerm.Name + " not found, creating it"); TermOperation op = new TermOperation(); op.Term = sourceTerm.Name; op.Id = _changeItem.Id.ToString(); op.Operation = "Add"; op.Type = "Term"; _list.Add(op); } catch { } } } } } } if (noError) { targetClientContext.ExecuteQuery(); targetTermStore.CommitAll(); } } }
/// <summary> /// Fetches the term store data for the term group specified /// </summary> /// <param name="clientContext">Client Context</param> /// <param name="termGroup">Term Group from the Term store</param> /// <param name="termStoreDetails">Details of the Term Group</param> /// <param name="returnFlagStatus">hierarchy of the Term Group</param> /// <returns>Hierarchy of the Term Group</returns> private TaxonomyResponseVM FetchGroupTerms(TermGroup termGroup, TermStoreDetails termStoreDetails) { try { //As a first step, load Practice Group Term set and anf its child terms only and not its child child terms clientContext.Load( termGroup, group => group.Name, group => group.TermSets.Include( termSet => termSet.Name, termSet => termSet.Id, termSet => termSet.Terms.Include( term => term.Name, term => term.Id, term => term.TermsCount, term => term.CustomProperties ))); clientContext.ExecuteQuery(); //This loop of the term set will get all the client termset and practice group term sets foreach (TermSet termSet in termGroup.TermSets) { if (termSet.Name == termStoreDetails.TermSetName) { //This condition is if the UI is requesting practice group terms if (termStoreDetails.TermSetName == taxonomySettings.PracticeGroupTermSetName) { taxonomyResponseVM.TermSets = GetManagedTermSetHierarchy(clientContext, termSet, termStoreDetails); } //This condition is if the UI is requesting client terms and the clients are defined as term set and not terms else if (termStoreDetails.TermSetName == taxonomySettings.ClientTermSetName ) { taxonomyResponseVM.ClientTermSets = GetClientTermSetHierarchy(clientContext, termSet, termStoreDetails); } } } //if (termStoreDetails.TermSetName == taxonomySettings.ClientTermSetName && // taxonomySettings.ClientTermPath != ServiceConstants.CLIENT_TERM_PATH) //{ // //This condition is if the UI is requesting client terms and the clients are defined as terms and not term sets // foreach (TermSet termSet in termGroup.TermSets) // { // if (termSet.Name == taxonomySettings.ClientTermPath) // { // taxonomyResponseVM.ClientTermSets = GetClientTermSetHierarchy(clientContext, termSet, termStoreDetails); // } // } //} } catch(Exception ex) { customLogger.LogError(ex, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, logTables.SPOLogTable); throw; } return taxonomyResponseVM; }
/// <summary> /// Initializing Practice Group List By Loading TermGroup /// </summary> /// <param name="clientContext">Client Context</param> /// <param name="termGroup">Term Group</param> /// <param name="termSetName">Term Set Name</param> /// <returns>Practice Group List</returns> public static List<PracticeGroup> IntializePracticeGroupObject(ClientContext clientContext, TermGroup termGroup, string termSetName) { PracticeGroup practiceGroup; SubAreaOfLaw subAreaOfLawTerm; AreaOfLaw areaOfLawTerm; TermSet termSet = termGroup.TermSets.GetByName(termSetName); TermCollection termColl = termSet.Terms; List<PracticeGroup> practiceGroups = new List<PracticeGroup>(); LoadExecute(clientContext, termColl); foreach (Term prctcGrp in termColl) { // Initializing Practice Group practiceGroup = new PracticeGroup(); practiceGroup.Name = prctcGrp.Name; practiceGroup.Id = Convert.ToString(prctcGrp.Id, CultureInfo.InvariantCulture); practiceGroup.AreaOfLaw = new List<AreaOfLaw>(); practiceGroups.Add(practiceGroup); foreach (Term areaLaw in prctcGrp.Terms) { // Initializing Area of Law areaOfLawTerm = new AreaOfLaw(); areaOfLawTerm.Name = areaLaw.Name; areaOfLawTerm.Id = Convert.ToString(areaLaw.Id, CultureInfo.InvariantCulture); areaOfLawTerm.SubareaOfLaw = new List<SubAreaOfLaw>(); practiceGroup.AreaOfLaw.Add(areaOfLawTerm); foreach (Term subAreaOfLaw in areaLaw.Terms) { subAreaOfLawTerm = new SubAreaOfLaw(); subAreaOfLawTerm.Name = subAreaOfLaw.Name; subAreaOfLawTerm.Id = Convert.ToString(subAreaOfLaw.Id, CultureInfo.InvariantCulture); foreach (KeyValuePair<string, string> termProperty in subAreaOfLaw.CustomProperties) { if (termProperty.Key.Equals(Constants.ContentTypeDictKey)) { subAreaOfLawTerm.ContentType = termProperty.Value; } } areaOfLawTerm.SubareaOfLaw.Add(subAreaOfLawTerm); } } } return practiceGroups; }
public void Import(TermGroup parentGroup) { if (parentGroup == null || parentGroup.ServerObjectIsNull.Value) throw new ArgumentNullException("parentGroup", "The parent Group object is null."); LoadWorkingLanguage(parentGroup.TermStore); if (_xml.DocumentElement.Name == "TermSets") { XmlNodeList termSetNodes = _xml.SelectNodes("./TermSets/TermSet"); if (termSetNodes == null || termSetNodes.Count == 0) { _cmdlet.WriteWarning("No Term Set elements were defined in the import file for the Group."); return; } foreach (XmlElement termSetElement in termSetNodes) { Import(termSetElement, parentGroup); } } else if (_xml.DocumentElement.Name == "TermSet") { Import(_xml.DocumentElement, parentGroup); } parentGroup.TermStore.CommitAll(); parentGroup.Context.ExecuteQuery(); }
private XmlElement AddGroupElement(XmlElement parent, TermGroup group) { XmlElement element = _xml.CreateElement("Group"); if (parent == null) _xml.AppendChild(element); else { if (parent.Name == "Groups") parent.AppendChild(element); else { XmlElement groupsElement = parent.SelectSingleNode("./Groups") as XmlElement; if (groupsElement == null) { groupsElement = _xml.CreateElement("Groups"); parent.AppendChild(groupsElement); } groupsElement.AppendChild(element); } } element.SetAttribute("Id", group.Id.ToString()); element.SetAttribute("Name", group.Name); element.SetAttribute("Description", group.Description); element.SetAttribute("CreatedDate", group.CreatedDate.ToString()); element.SetAttribute("LastModifiedDate", group.LastModifiedDate.ToString()); element.SetAttribute("IsSystemGroup", group.IsSystemGroup.ToString()); element.SetAttribute("IsSiteCollectionGroup", group.IsSiteCollectionGroup.ToString()); return element; }
private void Export(XmlElement parentElement, TermGroup group) { XmlElement groupElement = GetGroupElement(parentElement, group); var termSets = group.TermSets; if (!group.IsObjectPropertyInstantiated("TermSets") || !group.TermSets.AreItemsAvailable) { _ctx.Load(termSets); _ctx.ExecuteQuery(); } foreach (TermSet termSet in termSets) { Export(groupElement, termSet); } }
private XmlElement GetGroupElement(XmlElement parent, TermGroup group) { if (parent == null) return AddGroupElement(parent, group); XmlElement groupElement = parent.SelectSingleNode("./Groups/Group[@Id='" + group.Id + "']") as XmlElement; if (groupElement == null) groupElement = AddGroupElement(parent, group); return groupElement; }
private void CreateNewTargetTermGroup(ClientContext sourceClientContext, ClientContext targetClientContext, TermGroup sourceTermGroup, TermStore targetTermStore, List<int> languagesToProcess) { TermGroup destinationTermGroup = targetTermStore.CreateGroup(sourceTermGroup.Name, sourceTermGroup.Id); if (!string.IsNullOrEmpty(sourceTermGroup.Description)) { destinationTermGroup.Description = sourceTermGroup.Description; } TermSetCollection sourceTermSetCollection = sourceTermGroup.TermSets; if (sourceTermSetCollection.Count > 0) { foreach (TermSet sourceTermSet in sourceTermSetCollection) { sourceClientContext.Load(sourceTermSet, set => set.Name, set => set.Description, set => set.Id, set => set.Contact, set => set.CustomProperties, set => set.IsAvailableForTagging, set => set.IsOpenForTermCreation, set => set.CustomProperties, set => set.Terms.Include( term => term.Name, term => term.Description, term => term.Id, term => term.IsAvailableForTagging, term => term.LocalCustomProperties, term => term.CustomProperties, term => term.IsDeprecated, term => term.Labels.Include(label => label.Value, label => label.Language, label => label.IsDefaultForLanguage))); sourceClientContext.ExecuteQuery(); TermSet targetTermSet = destinationTermGroup.CreateTermSet(sourceTermSet.Name, sourceTermSet.Id, targetTermStore.DefaultLanguage); targetClientContext.Load(targetTermSet, set => set.CustomProperties); targetClientContext.ExecuteQuery(); UpdateTermSet(sourceClientContext, targetClientContext, sourceTermSet, targetTermSet); foreach (Term sourceTerm in sourceTermSet.Terms) { Term reusedTerm = targetTermStore.GetTerm(sourceTerm.Id); targetClientContext.Load(reusedTerm); targetClientContext.ExecuteQuery(); Term targetTerm; if (reusedTerm.ServerObjectIsNull.Value) { try { targetTerm = targetTermSet.CreateTerm(sourceTerm.Name, targetTermStore.DefaultLanguage, sourceTerm.Id); targetClientContext.Load(targetTerm, term => term.IsDeprecated, term => term.CustomProperties, term => term.LocalCustomProperties); targetClientContext.ExecuteQuery(); UpdateTerm(sourceClientContext, targetClientContext, sourceTerm, targetTerm, languagesToProcess); } catch (ServerException ex) { if (ex.Message.IndexOf("Failed to read from or write to database. Refresh and try again.") > -1) { // This exception was due to caching issues and generally is thrown when there's term reuse accross groups targetTerm = targetTermSet.ReuseTerm(reusedTerm, false); } else { throw ex; } } } else { targetTerm = targetTermSet.ReuseTerm(reusedTerm, false); } targetClientContext.Load(targetTerm); targetClientContext.ExecuteQuery(); targetTermStore.UpdateCache(); //Refresh session and termstore references to force reload of the term just added. This is //needed cause there can be a update change event following next and without this trick //the newly created termset cannot be obtained from the server targetTermStore = GetTermStoreObject(targetClientContext); //recursively add the other terms ProcessSubTerms(sourceClientContext, targetClientContext, targetTermSet, targetTerm, sourceTerm, languagesToProcess, targetTermStore.DefaultLanguage); } } } targetClientContext.ExecuteQuery(); }
public TermSet CreateTermSet(TermGroup group, string termsetname) { Ctx.Load(group.TermSets); Ctx.ExecuteQuery(); TermSet set = group.TermSets.FirstOrDefault(x => x.Name == termsetname); if (set == null) { set = group.CreateTermSet(termsetname, Guid.NewGuid(), Settings.Instance.LCID); Ctx.Load(set); Ctx.ExecuteQuery(); } return set; }
private void CreateTargetNewTermGroup(ClientContext sourceClientContext, ClientContext targetClientContext, TermGroup sourceTermGroup, TermStore targetTermStore) { try { this._destinationTermGroup = targetTermStore.CreateGroup(sourceTermGroup.Name, sourceTermGroup.Id); if(!string.IsNullOrEmpty(sourceTermGroup.Description)) { this._destinationTermGroup.Description = sourceTermGroup.Description; } TermOperation _op = new TermOperation(); _op.Term = sourceTermGroup.Name; _op.Id = sourceTermGroup.Id.ToString(); _op.Operation = "Add"; _op.Type = "TermGroup"; this._termStoreOperations.Add(_op); TermSetCollection _sourceTermSetCollection = sourceTermGroup.TermSets; if (_sourceTermSetCollection.Count > 0) { foreach (TermSet _sourceTermSet in _sourceTermSetCollection) { sourceClientContext.Load(_sourceTermSet, set => set.Name, set => set.Description, set => set.Id, set => set.Terms.Include( term => term.Name, term => term.Id), term => term.Description, term => term.Contact); sourceClientContext.ExecuteQuery(); TermSet _targetTermSet = _destinationTermGroup.CreateTermSet(_sourceTermSet.Name, _sourceTermSet.Id, targetTermStore.DefaultLanguage); if(!string.IsNullOrEmpty(_sourceTermSet.Description)) { _targetTermSet.Description = _sourceTermSet.Description; } foreach(Term _sourceTerm in _sourceTermSet.Terms) { Term _targetTerm = _targetTermSet.CreateTerm(_sourceTerm.Name, targetTermStore.DefaultLanguage, _sourceTerm.Id); _op = new TermOperation(); _op.Term = _sourceTerm.Name; _op.Id = _sourceTerm.Id.ToString(); _op.Operation = "Add"; _op.Type = "Term"; this._termStoreOperations.Add(_op); } } } try { targetClientContext.ExecuteQuery(); targetTermStore.CommitAll(); } catch { throw; } } catch { throw; } }
private static void CreateTermSetAndTerms(ClientContext clientContext, XElement termSetElement, TermGroup termGroup) { clientContext.Load(termGroup.TermSets); clientContext.ExecuteQuery(); string termSetElementName = termSetElement.Attribute("Name").Value; string termSetElementGuid = termSetElement.Attribute("ID").Value; TermSet termSet = termGroup.TermSets.FirstOrDefault(e => e.Name.Equals(termSetElementName) == true); if (termSet == null) { termSet = termGroup.CreateTermSet(termSetElementName, new Guid(termSetElementGuid), 1033); } bool isNavTermSet = Convert.ToBoolean(termSetElement.Attribute("IsForSiteNav").Value); if (isNavTermSet) { //set term set to work for site navigation termSet.SetCustomProperty("_Sys_Nav_IsNavigationTermSet", "True"); } clientContext.Load(termSet);clientContext.Load(termSet.Terms); clientContext.ExecuteQuery(); foreach (XElement termElement in termSetElement.Elements()) { #region set properties for each Term string termElementName = termElement.Attribute("Name").Value; Term term = termSet.Terms.FirstOrDefault(e => e.Name.Equals(termElementName) == true); if (term == null) { term = termSet.CreateTerm(termElementName, 1033, Guid.NewGuid()); } //clientContext.Load(term); clientContext.ExecuteQuery(); if (termElement.Attribute("NavNodeTitle").Value.Length>0) { term.SetLocalCustomProperty("_Sys_Nav_Title", termElement.Attribute("NavNodeTitle").Value); } bool showinGlobal = Convert.ToBoolean(termElement.Attribute("ShowinGlobal").Value); bool showinLocal = Convert.ToBoolean(termElement.Attribute("ShowinLocal").Value); if (!showinGlobal || !showinLocal) { if(!showinGlobal && !showinLocal) { term.SetLocalCustomProperty("_Sys_Nav_ExcludedProviders", String.Concat("GlobalNavigationTaxonomyProvider", ",", "CurrentNavigationTaxonomyProvider")); } if(!showinGlobal) { term.SetLocalCustomProperty("_Sys_Nav_ExcludedProviders", "\"GlobalNavigationTaxonomyProvider\""); } else { term.SetLocalCustomProperty("_Sys_Nav_ExcludedProviders", "\"CurrentNavigationTaxonomyProvider\""); } } if(termElement.Attribute("NavNodeSimpleLink").Value.Length>0) { term.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl", termElement.Attribute("NavNodeSimpleLink").Value); } if(termElement.Attribute("termFriendlyURLLink").Value.Length>0) { term.SetLocalCustomProperty("_Sys_Nav_FriendlyUrlSegment", termElement.Attribute("termFriendlyURLLink").Value); } if (termElement.Attribute("NavTargetURL").Value.Length > 0) { term.SetLocalCustomProperty("_Sys_Nav_TargetUrl", termElement.Attribute("NavTargetURL").Value); } clientContext.Load(term);clientContext.ExecuteQuery(); #endregion } }
private void Import(XmlElement termSetElement, TermGroup parentGroup) { string termSetName = termSetElement.GetAttribute("Name"); LoadWorkingLanguage(parentGroup.TermStore); TermSet termSet = null; ExceptionHandlingScope scope = new ExceptionHandlingScope(_ctx); using (scope.StartScope()) { using (scope.StartTry()) { termSet = parentGroup.TermSets.GetByName(termSetName); _ctx.Load(termSet); } using (scope.StartCatch()) { } } _ctx.ExecuteQuery(); if (termSet == null || termSet.ServerObjectIsNull == null || termSet.ServerObjectIsNull.Value) { _cmdlet.WriteVerbose(string.Format("Creating Term Set: {0}", termSetName)); int lcid = _workingLanguage; Guid id = Guid.NewGuid(); if (!string.IsNullOrEmpty(termSetElement.GetAttribute("Id"))) id = new Guid(termSetElement.GetAttribute("Id")); termSet = parentGroup.CreateTermSet(termSetName, id, lcid); if (!string.IsNullOrEmpty(termSetElement.GetAttribute("Contact"))) termSet.Contact = termSetElement.GetAttribute("Contact"); if (!string.IsNullOrEmpty(termSetElement.GetAttribute("Description"))) termSet.Description = termSetElement.GetAttribute("Description"); if (!string.IsNullOrEmpty(termSetElement.GetAttribute("CustomSortOrder"))) termSet.CustomSortOrder = termSetElement.GetAttribute("CustomSortOrder"); if (!string.IsNullOrEmpty(termSetElement.GetAttribute("IsAvailableForTagging"))) termSet.IsAvailableForTagging = bool.Parse(termSetElement.GetAttribute("IsAvailableForTagging")); if (!string.IsNullOrEmpty(termSetElement.GetAttribute("Owner"))) termSet.Owner = termSetElement.GetAttribute("Owner"); termSet.IsOpenForTermCreation = true; parentGroup.TermStore.CommitAll(); _ctx.Load(termSet); _ctx.ExecuteQuery(); } XmlNodeList stakeholderNodes = termSetElement.SelectNodes("./Stakeholders/Stakeholder"); if (stakeholderNodes != null && stakeholderNodes.Count > 0) { bool modified = false; foreach (XmlElement stakeholderElement in stakeholderNodes) { string stakeholder = stakeholderElement.GetAttribute("Value"); if (!termSet.Stakeholders.Contains(stakeholder)) { termSet.AddStakeholder(stakeholder); } } if (modified) _ctx.ExecuteQuery(); } XmlNodeList termsNodes = termSetElement.SelectNodes("./Terms/Term"); if (termsNodes != null && termsNodes.Count > 0) { foreach (XmlElement termElement in termsNodes) { Import(termElement, termSet); } } if (!string.IsNullOrEmpty(termSetElement.GetAttribute("IsOpenForTermCreation"))) { bool isOpenForTermCreation = bool.Parse(termSetElement.GetAttribute("IsOpenForTermCreation")); if (isOpenForTermCreation != termSet.IsOpenForTermCreation && termSet.Name != "Hashtags") { var orphanedTermsTermSet = termSet.TermStore.OrphanedTermsTermSet; termSet.Context.Load(orphanedTermsTermSet, o => o.Id); termSet.Context.ExecuteQuery(); if (orphanedTermsTermSet.Id != termSet.Id) { termSet.IsOpenForTermCreation = isOpenForTermCreation; termSet.Context.ExecuteQuery(); } } } }