0

I currently have a structure that has the following structure

Offices --> which is a Term

  • Region 1
              Area 1
                Town 1
                Town 2
             Area 2
                Town 1
    Region 2
              Area 1
                Town 1
    

and so on

What I am aiming to do is to flatten out this hierarchy using CSOM so I can display it in a drop down list for an MVC app I am using.

My starting point was

    public TermCollection Offices()
    {
                var taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
                var termStore = taxonomySession.GetDefaultKeywordsTermStore();
                var parentTermId = Guid.Parse("0d874cd3-da81-460a-8f49-a2286601654b");
                var parentTerm = termStore.GetTerm(parentTermId);
                officeTerms = parentTerm.Terms;
                clientContext.Load(officeTerms);
                clientContext.ExecuteQuery();
    }
}

I then return the officeTerms collection and wrap it in a selectList

This only loads the terms at the Region level. After a bit of digging it seemed that I need to recursively load any terms that have children so I added LoadTerms(officeTerms) after ExecuteQuery above to give me

        public TermCollection Offices()
        {
            var taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
            var termStore = taxonomySession.GetDefaultKeywordsTermStore();
            var parentTermId = Guid.Parse("0d874cd3-da81-460a-8f49-a2286601654b");
            var parentTerm = termStore.GetTerm(parentTermId);
            officeTerms = parentTerm.Terms;
            clientContext.Load(officeTerms);
            clientContext.ExecuteQuery();
            LoadTerms(officeTerms)
}

and this method

private void LoadTerms(TermCollection offices, ClientContext context)
        {
            TermCollection terms;
            foreach (var office in offices)
            {
                TermCollection termCollection = office.Terms;
                if (practice.TermsCount > 0)
                {
                    context.Load(termCollection, tc => tc.Include(t => t.TermsCount,t => t.Terms, t => t.Name));
                    context.ExecuteQuery();
                    LoadTerms(termCollection, context);
                }
            }
        }

The problem with this of that when I call LoadTerms the officeTerms collection is no longer referenced so I cannot include the children.

My question is how do I get the Offices method to load all children of all terms and return that to flatten the list.

Thanks for any help with this

1

If you can change the design to make Offices as a TermSet instead of Term , you can use TermSet.GetAllTerms which will give all terms inside a termset in flat hierarchy.

Or you could add an extra filter on the result returned by TermSet.GetAllTerms.

 private static void getTerms()
        {
            var taxonomySession = TaxonomySession.GetTaxonomySession(ctx);
            var termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
            TermGroup termGroup = termStore.Groups.GetByName("<groupName>");
            // Get the term set by Name
            TermSet termSet = termGroup.TermSets.GetByName("<termSetName>");
            TermCollection termColl = termSet.GetAllTerms();
            var allTerms=ctx.LoadQuery(termColl);
            ctx.ExecuteQuery();
            //get only the terms inside another term 
            foreach (var term in allTerms.Where(t=> t.PathOfTerm.StartsWith("<parentTerm>;")))
            {
                Console.WriteLine(term.Name);
            }
        }
  • Quite new to Sharepoint but am not sure I can do that as Office is in a Termset called Locations which also contains other hierarchical terms such as Departments. I get the feeling this would break the design. – lostinwpf Sep 14 '16 at 9:48
  • Updated my answer – Unnie Sep 14 '16 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.