static void Main(string[] args) { Log("\r\nTermSetSync (Client OM): Starting...\r\n"); // Uncomment this section to create a sample input file. #if false GenerateExampleXmlFile(); #else TermSetGoal termSetGoal = LoadTermSetGoal(); Program.clientContext = new ClientContext(siteUrl); TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(Program.clientContext); taxonomySession.UpdateCache(); TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore(); TermSet termSet = CreateTermSetAndGroup(termStore, termSetGoal); SyncTermSet(termSet, termSetGoal); #endif Log("\r\nThe operation completed successfully."); if (Debugger.IsAttached) { Debugger.Break(); } }
static void Main(string[] args) { Log("\r\nTermSetSync (Server OM): Starting...\r\n"); // Uncomment this section to create a sample input file. #if false GenerateExampleXmlFile(); #else TermSetGoal termSetGoal = LoadTermSetGoal(); using (SPSite site = new SPSite(siteUrl)) { TaxonomySession taxonomySession = new TaxonomySession(site, updateCache: true); TermStore termStore = taxonomySession.DefaultSiteCollectionTermStore; Program.lcid = termStore.WorkingLanguage; SyncTermSet(termStore, termSetGoal); } #endif Log("\r\nThe operation completed successfully."); if (Debugger.IsAttached) { Debugger.Break(); } }
// Update the objects in the Taxonomy TermSet to match the objects specified // in the TermSetGoal object that was read from the XML file. static void SyncTermSet(TermSet termSet, TermSetGoal termSetGoal) { // Load the tree of terms as a flat list. Log("Loading terms..."); TermCollection allTerms = termSet.GetAllTerms(); Program.clientContext.Load(allTerms, allTermsArg => allTermsArg.Include(Program.termRetrievals) ); Program.ExecuteQuery(); // WCF CALL #2 Program.itemsById = new Dictionary <Guid, Wrapper>(); Wrapper termSetWrapper = new Wrapper(termSet, termSet.Id, Guid.Empty, termSet.Name); Program.itemsById.Add(termSetWrapper.Id, termSetWrapper); foreach (Term term in allTerms) { Guid parentItemId = termSet.Id; if (!term.Parent.ServerObjectIsNull.Value) { parentItemId = term.Parent.Id; } Program.itemsById.Add(term.Id, new Wrapper(term, term.Id, parentItemId, term.Name)); } foreach (Wrapper wrapper in Program.itemsById.Values) { if (wrapper != termSetWrapper) { Program.itemsById[wrapper.ParentId].ChildTerms.Add(wrapper); } } Log("Step 1: Adds and Moves"); ProcessAddsAndMoves(termSetWrapper, termSetGoal); Log("Step 2: Deletes"); ProcessDeletes(termSetWrapper, termSetGoal); // Step 2 Log("Step 3: Property Updates"); Log("Querying auxiliary data"); foreach (Wrapper wrapper in Program.itemsById.Values) { if (wrapper != termSetWrapper) { wrapper.TermDescription = ((Term)wrapper.Item).GetDescription(Program.lcid); } } Program.ExecuteQuery(); // WCF CALL #3 ProcessPropertyUpdates(termSetWrapper, termSetGoal); // Step 3 Program.ExecuteQuery(); // WCF CALL #4 }
// Create the TermSetGoal objects in the specified TermStore object. static void SyncTermSet(TermStore termStore, TermSetGoal termSetGoal) { Group group = termStore.Groups.FirstOrDefault(g => g.Name == termSetGoal.GroupName); if (group == null) { Log("* Creating missing group \"" + termSetGoal.GroupName + "\""); group = termStore.CreateGroup(termSetGoal.GroupName); } TermSet termSet = termStore.GetTermSet(termSetGoal.Id); if (termSet == null) { Log("* Creating missing term set \"" + termSetGoal.Name + "\""); termSet = group.CreateTermSet(termSetGoal.Name, termSetGoal.Id, lcid); } else { if (termSet.Group.Id != group.Id) { Log("* Moving term set to group \"" + group.Name + "\""); termSet.Move(group); } if (termSet.Name != termSetGoal.Name) { termSet.Name = termSetGoal.Name; } } termStore.CommitAll(); // Load the tree of terms as a flat list. Dictionary <Guid, Term> termsById = new Dictionary <Guid, Term>(); foreach (Term term in termSet.GetAllTerms()) { termsById.Add(term.Id, term); } Log("Step 1: Adds and Moves"); ProcessAddsAndMoves(termSet, termSetGoal, termsById); Log("Step 2: Deletes"); // Requery the TermSet object to reflect changes to the topology. termSet = termStore.GetTermSet(termSetGoal.Id); ProcessDeletes(termSet, termSetGoal); // Step 2 Log("Step 3: Property Updates"); termSet = termStore.GetTermSet(termSetGoal.Id); ProcessPropertyUpdates(termSet, termSetGoal); // Step 3 termStore.CommitAll(); }
// This function uses the XmlSerializer object to write an example input file // that illustrates the XML syntax. static void GenerateExampleXmlFile() { TermSetGoal termSet = new TermSetGoal(); termSet.Name = "TermSet"; var parentTerm = new TermGoal() { Name = "Term1" }; termSet.TermGoals.Add(parentTerm); parentTerm.TermGoals.Add(new TermGoal() { Name = "Term2", OtherLabels = new List <string>(new[] { "A", "B", "C" }) }); XmlSerializer serializer = new XmlSerializer(typeof(TermSetGoal)); using (Stream stream = new FileStream("ExampleInput.xml", FileMode.Create)) { serializer.Serialize(stream, termSet); } }
// If Term and TermGroup objects do not already exist, this function creates them as // specified by the TermSetGoal object that was read from the XML // input file. The code also calls the ClientContext.Load() method to query // client object model properties that will be needed later. static TermSet CreateTermSetAndGroup(TermStore termStore, TermSetGoal termSetGoal) { // Load the data Program.clientContext.Load(termStore, termStoreArg => termStoreArg.WorkingLanguage, termStoreArg => termStoreArg.Groups.Include( groupArg => groupArg.Id, groupArg => groupArg.Name ) ); TermSet termSet = termStore.GetTermSet(termSetGoal.Id); Program.clientContext.Load(termSet, termSetArg => termSetArg.Id, termSetArg => termSetArg.Name, termSetArg => termSetArg.Group.Id); Program.ExecuteQuery(); // WCF CALL #1 // Set up the group and the term set. Program.lcid = termStore.WorkingLanguage; TermGroup group = termStore.Groups.ToList().FirstOrDefault(g => g.Name == termSetGoal.GroupName); if (group == null) { // (ServerObjectIsNull is not used here) Log("* Creating missing group \"" + termSetGoal.GroupName + "\""); group = termStore.CreateGroup(termSetGoal.GroupName, Guid.NewGuid()); } if (termSet.ServerObjectIsNull.Value) { Log("* Creating missing term set \"" + termSetGoal.Name + "\""); termSet = group.CreateTermSet(termSetGoal.Name, termSetGoal.Id, Program.lcid); Program.clientContext.Load(termSet, termSetArg => termSetArg.Id, termSetArg => termSetArg.Name, termSetArg => termSetArg.Group.Id); } #if false else { if (termSet.Group.Id != group.Id) { Log("* Moving term set to group \"" + group.Name + "\""); termSet.Move(group); } else { Log("Verified term set \"" + termSetGoal.Name + "\""); } if (termSet.Name != termSetGoal.Name) { termSet.Name = termSetGoal.Name; } } #endif return(termSet); }