TermGroup

Namespace: Omnia.Fx.Models
We found 10 examples in language CSharp for this search. You will see 38 fragments of code.

        public static ObservableCollection<TermGroup> ConvertToTermGroups(ObservableCollection<Term> terms)
        {
            ObservableCollection<TermGroup> groups = new ObservableCollection<TermGroup>();
            foreach (var term in terms)
            {
                groups.Add(ConvertToTermGroup(term));
            }
            return groups;
        }

        public static TermGroup ConvertToTermGroup(Term term)
        {
            return new TermGroup(term);

        }


        public void ClearTerms(string termGroup, string termSet) {
            var set = getTermSet(termGroup, termSet);
            var terms = set.GetAllTerms();
            _ctx.Load(terms);
            _ctx.ExecuteQuery();

            foreach (var term in terms) {
                Console.Write("Deleting {0} ... ", term.Name);
                term.DeleteObject();
                _ctx.ExecuteQuery();
                Console.WriteLine("Done");
            }
        }


        /// <summary>Gets the determined first token sets for the grammar.</summary>
        /// <param name="item">This is the item to get the token set for.</param>
        /// <param name="tokens">The set to add the found tokens to.</param>
        /// <returns>True if the item has a lambda, false otherwise.</returns>
        public bool Firsts(Item item, HashSet<TokenItem> tokens) {
            if (item is TokenItem tItem) {
                tokens.Add(tItem);
                return false;
            }
            
            if (item is Term term) {
                TermGroup group = this.terms[term];
                foreach (TokenItem token in group.Tokens)
                    tokens.Add(token);
                return group.HasLambda;
            }
            
            return false; // Prompt
        }

        /// <summary>Joins these two groups as parent and dependent.</summary>
        /// <param name="parent">The parent to join to a dependent.</param>
        /// <param name="dep">The dependent to join to the parent.</param>
        static private void joinGroups(TermGroup parent, TermGroup dep) {
            parent.Dependents.Add(dep);
            dep.Parents.Add(parent);
        }

        /// <summary>Propagates the rule information into the given group.</summary>
        /// <param name="group">The group to add the rule information to.</param>
        /// <param name="rule">The rule to add token firsts into the group.</param>
        /// <returns>True if the group has been changed, false otherwise.</returns>
        private bool propageteRule(TermGroup group, Rule rule) {
            bool updated = false;
            foreach (Item item in rule.Items) {

                // Check if token, if so skip the lambda check and just leave.
                if (item is TokenItem tItem)
                    return group.Tokens.Add(tItem);

                // If term, then join to all the parents
                if (item is Term term) {
                    TermGroup parent = this.terms[term];
                    joinGroups(parent, group);
                    foreach (TermGroup grand in parent.Parents)
                        joinGroups(grand, group);
                    foreach (TokenItem token in parent.Tokens) {
                        if (group.Tokens.Add(token)) updated = true;
                    }
                    if (!parent.HasLambda) return updated;
                }

                // else ignore because it is Prompt
            }

            // If the end has been reached with out stopping
            if (!group.HasLambda) {
                group.HasLambda = true;
                updated = true;
            }
            return updated;
        }

        /// <summary>Propagate all the rules for the given term.</summary>
        /// <param name="term">The term to propagate.</param>
        /// <returns>True if the group has been changed, false otherwise.</returns>
        private bool propagate(Term term) {
            TermGroup group = this.terms[term];
            if (!group.Update) return false;
            group.Update = false;

            // Run through all rules and update with them.
            bool updated = false;
            foreach (Rule rule in term.Rules) {
                if (this.propageteRule(group, rule)) updated = true;
            }

            // Mark all dependents as needing updates.
            if (updated) {
                foreach (TermGroup dep in group.Dependents)
                    dep.Update = true;
            }
            return updated;
        }

        /// <summary>Gets a string for debugging the grammar's first tokens.</summary>
        /// <returns>The string with the first tokens.</returns>
        public override string ToString() {
            int maxWidth = 0;
            foreach (Term term in this.terms.Keys)
                maxWidth = Math.Max(maxWidth, term.Name.Length);

            string[] parts = new string[this.terms.Count];
            int i = 0;
            foreach (TermGroup group in this.terms.Values) {
                string firstStr = "";
                if (group.Tokens.Count > 0) {
                    string[] firsts = new string[group.Tokens.Count];
                    int j = 0;
                    foreach (TokenItem item in group.Tokens) {
                        firsts[j] = item.Name;
                        ++j;
                    }
                    Array.Sort(firsts);
                    firstStr = "["+string.Join(", ", firsts) +"]";
                }
                string lambda = group.HasLambda ? " λ": "";
                parts[i] = group.Term.Name.PadRight(maxWidth) + " → " + firstStr + lambda;
                ++i;
            }
            Array.Sort(parts);
            return string.Join(Environment.NewLine, parts);
        }

        public int ImportTerms(MMSImportOptions opts) {
            try {
                // parse csv
                var data = DataTable.New.ReadCsv(opts.FilePath);
                var terms = new List<TermData>();
                foreach (var row in data.Rows) {
                    var term = new TermData {
                        Term = row.GetValueOrEmpty("Term")
                    };
                    foreach (var col in data.ColumnNames) {
                        if (col.StartsWith("Prop_")) {
                            var prop = col.Substring(5);
                            term.Properties[prop] = row.GetValueOrEmpty(col);
                        }
                    }
                    terms.Add(term);
                }

                WithContext(opts, ctx => {
                    // import terms
                    var mgr = new MetadataManager(ctx, opts.TermStore);
                    mgr.ImportTerms(opts.TermGroup, opts.TermSet, terms);
                });

            } catch (Exception ex) {
                Console.WriteLine("An error occurred");
                Console.WriteLine(ex.Message);
                return 1;
            }
            return 0;
        }

        public int ExportTerms(MMSExportOptions opts) {
            try {
                IDictionary<string, string[]> terms = null;
                WithContext(opts, ctx => {
                    var mgr = new MetadataManager(ctx, opts.TermStore);
                    terms = mgr.ExportTerms(opts.TermGroup, opts.TermSet);
                });

                var flat = terms.SelectMany(kvp => {
                    return kvp.Value.Select(t => {
                        return new {
                            TermSet = kvp.Key,
                            Term = t
                        };
                    });
                });

                DataTable.New.FromEnumerable(flat).SaveCSV(opts.FilePath);
                
            } catch (Exception ex) {
                Console.WriteLine("An error occurred");
                Console.WriteLine(ex.Message);
                return 1;
            }
            return 0;
        }

        public int ClearTerms(MMSClearOptions opts) {
            try {
                WithContext(opts, ctx => {
                    var mgr = new MetadataManager(ctx, opts.TermStore);
                    mgr.ClearTerms(opts.TermGroup, opts.TermSet);
                });
            } catch (Exception ex) {
                Console.WriteLine("An error occurred");
                Console.WriteLine(ex.Message);
                return 1;
            }

            return 0;
        }


        public override void OnTenantRunningStart(Tenant tenant)
        {
            tenant.Context.Load(tenant,
                t => t.RootSiteUrl);
            tenant.Context.ExecuteQuery();

            string managedMetadataFileName = $"AuditManagedMetadata--{DateTime.Now.ToString("yyyy-MM-dd HH,mm,ss")}.csv";
            managedMetadataFileWriter = new CsvWriterWrapper<TermInfo, TermInfoMap>(managedMetadataFileName);
        }

        public override void OnTermStoreRunningStart(TermStore termStore)
        {
            termStore.Context.Load(termStore,
                s => s.Name);
            termStore.Context.ExecuteQuery();

            TermInfo termInfo = new TermInfo()
            {
                Type = Type.TermStore,
                Name = termStore.Name
            };

            // Write CSV
            managedMetadataFileWriter.WriteRecord(termInfo);
        }

        public override void OnTermGroupRunningStart(TermGroup termGroup)
        {
            termGroup.Context.Load(termGroup,
                s => s.Name);
            termGroup.Context.ExecuteQuery();

            TermInfo termInfo = new TermInfo()
            {
                Type = Type.TermGroup,
                Name = termGroup.Name
            };

            // Write CSV
            managedMetadataFileWriter.WriteRecord(termInfo);
        }

        public override void OnTermSetRunningStart(TermSet termSet)
        {
            termSet.Context.Load(termSet,
                s => s.Name);
            termSet.Context.ExecuteQuery();

            TermInfo termInfo = new TermInfo()
            {
                Type = Type.TermSet,
                Name = termSet.Name
            };

            // Write CSV
            managedMetadataFileWriter.WriteRecord(termInfo);
        }

        public override void OnTermRunningStart(Term term)
        {
            term.Context.Load(term,
                s => s.Name);
            term.Context.ExecuteQuery();

            TermInfo termInfo = new TermInfo()
            {
                Type = Type.Term,
                Name = term.Name
            };

            // Write CSV
            managedMetadataFileWriter.WriteRecord(termInfo);
        }


        public override void OnTenantRunningStart(Tenant tenant)
        {
            tenant.Context.Load(tenant,
                t => t.RootSiteUrl);
            tenant.Context.ExecuteQuery();

            string managedMetadataFileName = $"AuditManagedMetadata--{DateTime.Now.ToString("yyyy-MM-dd HH,mm,ss")}.csv";
            managedMetadataFileWriter = new CsvWriterWrapper<TermInfo, TermInfoMap>(managedMetadataFileName);
        }

        public override void OnTermStoreRunningStart(TermStore termStore)
        {
            termStore.Context.Load(termStore,
                s => s.Name);
            termStore.Context.ExecuteQuery();

            TermInfo termInfo = new TermInfo()
            {
                Type = Type.TermStore,
                Name = termStore.Name
            };

            // Write CSV
            managedMetadataFileWriter.WriteRecord(termInfo);
        }

        public override void OnTermGroupRunningStart(TermGroup termGroup)
        {
            termGroup.Context.Load(termGroup,
                s => s.Name);
            termGroup.Context.ExecuteQuery();

            TermInfo termInfo = new TermInfo()
            {
                Type = Type.TermGroup,
                Name = termGroup.Name
            };

            // Write CSV
            managedMetadataFileWriter.WriteRecord(termInfo);
        }

        public override void OnTermSetRunningStart(TermSet termSet)
        {
            termSet.Context.Load(termSet,
                s => s.Name);
            termSet.Context.ExecuteQuery();

            TermInfo termInfo = new TermInfo()
            {
                Type = Type.TermSet,
                Name = termSet.Name
            };

            // Write CSV
            managedMetadataFileWriter.WriteRecord(termInfo);
        }

        public override void OnTermRunningStart(Term term)
        {
            term.Context.Load(term,
                s => s.Name);
            term.Context.ExecuteQuery();

            TermInfo termInfo = new TermInfo()
            {
                Type = Type.Term,
                Name = term.Name
            };

            // Write CSV
            managedMetadataFileWriter.WriteRecord(termInfo);
        }


        /// <summary>
        /// Action for this SharePoint term group
        /// </summary>
        public override void Process()
        {
            RunningManager.Logger.Debug($"TermGroupRunner Process() - {ActiveReceivers.Count} active receivers");
            Context.Load(Element,
                g => g.Name);
            Context.ExecuteQuery();
            RunningManager.Logger.Debug($"TermGroup | Name: {Element.Name}");

            // OnTermGroupRunningStart
            RunningManager.Logger.Debug("TermGroupRunner OnTermGroupRunningStart()");
            ActiveReceivers.ForEach(r => r.OnTermGroupRunningStart(Element));

            // If at least one receiver run term sets or deeper
            if (Manager.Receivers.Any(r => r.IsReceiverCalledOrDeeper(RunningLevel.TermSet)))
            {
                // Crawl term sets
                Context.Load(Element.TermSets);
                Context.ExecuteQuery();

                List<TermSetRunner> termSetRunners = new List<TermSetRunner>();
                foreach (TermSet set in Element.TermSets)
                {
                    termSetRunners.Add(new TermSetRunner(Manager, Context, set));
                }

                termSetRunners.ForEach(r => r.Process());
            }

            // OnTermGroupRunningEnd
            RunningManager.Logger.Debug("TermGroupRunner OnTermGroupRunningEnd()");
            ActiveReceivers.ForEach(r => r.OnTermGroupRunningEnd(Element));
        }

        public static void SetupTermStore(ClientContext clientContext)
        {
            var taxSession = TaxonomySession.GetTaxonomySession(clientContext);
            var termStore = taxSession.GetDefaultSiteCollectionTermStore();

            AddLanguages(clientContext, termStore);
            var termGroup = AddGroup(clientContext, termStore);
            var termSet = AddTermSet(clientContext, termStore, termGroup);

            AddTerm(clientContext, termSet, "Departments", "Départements", "Abteilungen", "Avdelningar");
            AddTerm(clientContext, termSet, "Employee", "Employé", "Mitarbeiter", "Anställd");
            AddTerm(clientContext, termSet, "News", "Actualités", "Nachricht", "Nyheter");
            AddTerm(clientContext, termSet, "Search", "Rechercher", "Suche", "Sök");
        }

        private static void AddTerm(ClientContext clientContext, TermSet termSet, string termName, string termNameFrench, string termNameGerman, string termNameSwedish)
        {
            if (!termSet.Terms.Any(t => t.Name.Equals(termName)))
            {
                var term = termSet.CreateTerm(termName, 1033, Guid.NewGuid());
                term.CreateLabel(termNameGerman, 1031, false);
                term.CreateLabel(termNameFrench, 1036, false);
                term.CreateLabel(termNameSwedish, 1053, false);
                term.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl", clientContext.Web.ServerRelativeUrl);
                clientContext.ExecuteQuery();
            }
        }

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

        private static TermGroup AddGroup(ClientContext clientContext, TermStore termStore)
        {
            var groupId = new Guid("8de44223-5a8f-41cd-b0e2-5634b0bb953b");
            var termGroup = termStore.GetGroup(groupId);
            clientContext.Load(termGroup);
            clientContext.ExecuteQuery();

            if (termGroup.ServerObjectIsNull.Value)
            {
                termGroup = termStore.CreateGroup("Taxonomy Navigation", groupId);
                clientContext.Load(termGroup);
                clientContext.ExecuteQuery();
            }

            return termGroup;
        }

        private static void AddLanguages(ClientContext clientContext, TermStore termStore)
        {
            clientContext.Load(clientContext.Web, w => w.ServerRelativeUrl);
            clientContext.Load(termStore, ts => ts.Languages);
            clientContext.ExecuteQuery();

            var languages = new int[] { 1031, 1033, 1036, 1053 };
            Array.ForEach(languages, l =>
            {
                if (!termStore.Languages.Contains(l))
                    termStore.AddLanguage(l);
            });

            termStore.CommitAll();
            clientContext.ExecuteQuery();
        }

        public static void SetupTermStore(ClientContext clientContext)
        {
            var taxSession = TaxonomySession.GetTaxonomySession(clientContext);
            var termStore = taxSession.GetDefaultSiteCollectionTermStore();

            AddLanguages(clientContext, termStore);
            var termGroup = AddGroup(clientContext, termStore);
            var termSet = AddTermSet(clientContext, termStore, termGroup);

            AddTerm(clientContext, termSet, "Departments", "Départements", "Abteilungen", "Avdelningar");
            AddTerm(clientContext, termSet, "Employee", "Employé", "Mitarbeiter", "Anställd");
            AddTerm(clientContext, termSet, "News", "Actualités", "Nachricht", "Nyheter");
            AddTerm(clientContext, termSet, "Search", "Rechercher", "Suche", "Sök");
        }

        private static void AddTerm(ClientContext clientContext, TermSet termSet, string termName, string termNameFrench, string termNameGerman, string termNameSwedish)
        {
            if (!termSet.Terms.Any(t => t.Name.Equals(termName)))
            {
                var term = termSet.CreateTerm(termName, 1033, Guid.NewGuid());
                term.CreateLabel(termNameGerman, 1031, false);
                term.CreateLabel(termNameFrench, 1036, false);
                term.CreateLabel(termNameSwedish, 1053, false);
                term.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl", clientContext.Web.ServerRelativeUrl);
                clientContext.ExecuteQuery();
            }
        }

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

        private static TermGroup AddGroup(ClientContext clientContext, TermStore termStore)
        {
            var groupId = new Guid("8de44223-5a8f-41cd-b0e2-5634b0bb953b");
            var termGroup = termStore.GetGroup(groupId);
            clientContext.Load(termGroup);
            clientContext.ExecuteQuery();

            if (termGroup.ServerObjectIsNull.Value)
            {
                termGroup = termStore.CreateGroup("Taxonomy Navigation", groupId);
                clientContext.Load(termGroup);
                clientContext.ExecuteQuery();
            }

            return termGroup;
        }

        private static void AddLanguages(ClientContext clientContext, TermStore termStore)
        {
            clientContext.Load(clientContext.Web, w => w.ServerRelativeUrl);
            clientContext.Load(termStore, ts => ts.Languages);
            clientContext.ExecuteQuery();

            var languages = new int[] { 1031, 1033, 1036, 1053 };
            Array.ForEach(languages, l =>
            {
                if (!termStore.Languages.Contains(l))
                    termStore.AddLanguage(l);
            });

            termStore.CommitAll();
            clientContext.ExecuteQuery();
        }

        /// <summary>
        /// Gets the term store identified by its Guid.
        /// </summary>
        /// <param name="clientContext">The client context.</param>
        /// <param name="termStoreGuid">The term store unique identifier.</param>
        /// <returns></returns>
        public static TermStore GetTermStoreByGuid(ClientContext clientContext, Guid termStoreGuid)
        {
            var taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
            var termStore = taxonomySession.TermStores.GetById(termStoreGuid);
            clientContext.Load(termStore);
            clientContext.ExecuteQuery();
            return termStore;
        }

        /// <summary>
        /// Gets the first or default term store.
        /// </summary>
        /// <param name="clientContext">The client context.</param>
        /// <returns></returns>
        public static TermStore GetFirstOrDefaultTermStore(ClientContext clientContext)
        {
            var taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
            clientContext.Load(taxonomySession.TermStores);
            clientContext.ExecuteQuery();

            var termStore = taxonomySession.TermStores.FirstOrDefault();
            if (termStore != null)
            {
                clientContext.Load(termStore);
                clientContext.ExecuteQuery();
                return termStore;
            }
            return null;
        }

        /// <summary>
        /// Gets all the term stores with the given name.
        /// </summary>
        /// <param name="clientContext">The client context.</param>
        /// <param name="termStoreName">Name of the term store.</param>
        /// <returns></returns>
        public static List<TermStore> GetTermStoresByName(ClientContext clientContext, string termStoreName)
        {
            var taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
            var termStores = taxonomySession.TermStores;
            return termStores.Where(termStore => termStoreName.Equals(termStore.Name)).ToList();
        }

        /// <summary>
        /// Gets the termstore group by name.
        /// </summary>
        /// <param name="clientContext">The client context.</param>
        /// <param name="termStore">The term store.</param>
        /// <param name="termStoreGroupName">Name of the term store group.</param>
        /// <returns></returns>
        public static TermGroup GetTermGroupByName(ClientContext clientContext, TermStore termStore, string termStoreGroupName)
        {
            if (!Contains(clientContext, termStore, termStoreGroupName))
                return null;

            var termGroup = termStore.Groups.GetByName(termStoreGroupName);
            clientContext.Load(termGroup);
            clientContext.ExecuteQuery();
            return termGroup;
        }

        /// <summary>
        /// Gets the term set.
        /// </summary>
        /// <param name="clientContext">The client context.</param>
        /// <param name="termGroup">The term group.</param>
        /// <param name="termSetName">Name of the term set.</param>
        /// <returns></returns>
        public static TermSet GetTermSetByName(ClientContext clientContext, TermGroup termGroup, string termSetName)
        {
            var termSet = termGroup.TermSets.GetByName(termSetName);
            clientContext.Load(termSet);
            clientContext.ExecuteQuery();
            return termSet;
        }

        /// <summary>
        /// Gets the term by name.
        /// </summary>
        /// <param name="clientContext">The client context.</param>
        /// <param name="termSet">The term set.</param>
        /// <param name="termName">Name of the term.</param>
        /// <returns></returns>
        public static Term GetTermByName(ClientContext clientContext, TermSet termSet, string termName)
        {
            var term = termSet.Terms.GetByName(termName);
            clientContext.Load(term);
            clientContext.ExecuteQuery();
            return term;
        }

Omnia.Fx.Models.Taxonomy.TermGroup : Object

Constructors :

public TermGroup()

Methods :

public Guid get_Id()
public Void set_Id(Guid value = )
public Guid get_TermStoreId()
public Void set_TermStoreId(Guid value = )
public String get_Name()
public Void set_Name(String value = )
public IList<TermSet> get_TermSets()
public Void set_TermSets(IList<TermSet> value = )
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()