/// <summary> /// Deletes terms from term stores /// </summary> /// <param name="TermsData">Data to be delete from term store</param> /// <param name="clientContext">Client context object</param> private static void DeleteMatterCenterTerms(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 groupCurrent = termStore.Groups.Where(termGroup => termGroup.Name == cltGroup.name).Count() > 0 ? termStore.Groups.GetByName(cltGroup.name) : termStore.CreateGroup(cltGroup.name, Guid.NewGuid()); clientContext.Load(groupCurrent.TermSets); //Execute the query to the server clientContext.ExecuteQuery(); TermSet clientTerm = (from t in groupCurrent.TermSets where t.Name == ConfigurationManager.AppSettings["clientterm"] select t).FirstOrDefault(); TermSet clientID = (from t in groupCurrent.TermSets where t.Name == ConfigurationManager.AppSettings["clientidterm"] select t).FirstOrDefault(); TermSet PGTerm = (from t in groupCurrent.TermSets where t.Name == ConfigurationManager.AppSettings["pgterm"] select t).FirstOrDefault(); if (clientTerm != null) { Console.WriteLine("Deleting clients"); clientTerm.DeleteObject(); } if (clientID != null) { Console.WriteLine("Deleting client ids"); clientID.DeleteObject(); } if (PGTerm != null) { Console.WriteLine("Deleting practice groups"); PGTerm.DeleteObject(); } groupCurrent.DeleteObject(); } //Execute the query to the server clientContext.ExecuteQuery(); Console.WriteLine("Existing taxonomy hierarchy deleted successfully"); }
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(); } } }
public void Delete() { _termGroup.DeleteObject(); _termGroup.Context.ExecuteQuery(); }