How does one get all child terms of a SharePoint term in C#?

23,836

Solution 1

The function you are looking for is Term.GetTerms

You will need to get a TaxonomyValue from your field

Then you have to get the current TaxonomySession, then use the TaxonomySession to get the Term used in the field. From that term you can use the Parent field to get the parent Term. Here is some rough code to show you the objects used.

         TaxonomyFieldValue v = null; // Notsurehowtodothisbit();
        TaxonomySession session = new TaxonomySession(site);
        if (session.TermStores != null && session.TermStores.Count > 0)
        {

            TermStore termStore = session.TermStores[0];
            Term t = termStore.GetTerm(v.TermGuid);
            Term parentTerm = t.Parent;   
            TermCollection childTerms = t.GetTerms();
        }

Once you have the tree, you may be able to use a caml query to generate a SPList.GetList query that brings back anything tagged that way.

I have not done an experiment in this regard... But Bart-Jan Hoeijmakers has

    private SPListItemCollection GetItemsByTerm(Term term, SPList list)
    {
        // init some vars    SPListItemCollection items = null;    
        SPSite site = SPContext.Current.Site;     // set up the TaxonomySession    
        TaxonomySession session = new TaxonomySession(site);
        // get the default termstore    TermStore termStore = session.TermStores[0];   
        // If no wssid is found, the term is not used yet in the sitecollection, so no items exist using the term   
        int[] wssIds = TaxonomyField.GetWssIdsOfTerm(SPContext.Current.Site, termStore.Id, term.TermSet.Id, term.Id, false, 1);
        if (wssIds.Length > 0)
        {
            // a TaxonomyField is a lookupfield. Constructing the SPQuery       
            SPQuery query = new SPQuery();
            query.Query = String.Format("<Where><Eq><FieldRef Name='MyTaxonomyField' LookupId='TRUE' /><Value Type='Lookup'>{0}</Value></Eq></Where>", wssIds[0]);
            items = list.GetItems(query);
        }
        return items;
    }

Solution 2

Nat's partial answer using the GetTerms method for the parent is great. The code for querying one list looks good too.

To get the id for the parent term, you can use TermStore.GetTerms against the title.

To search across all lists and libraries in the the site collection, you can use the Search API's FullTextSQLQuery method specifying the guids in the where clause with the owstaxIdMyTaxonomyField as the column.

There is a great example of getting id's by title and searching for a term store by id at Using taxonomy fields in SharePoint 2010: Part III

Share:
23,836
Tom Macdonald
Author by

Tom Macdonald

I am a Operations Research and software engineer. I did my MSc in Nantes, France, my internship in Vienna, Austria, and come from London, UK. I am freelancing in Vienna. Feel free to contact me! I do anything interesting that's solvable in software, but also websites.

Updated on June 21, 2020

Comments

  • Tom Macdonald
    Tom Macdonald almost 4 years

    I am writing a webpart for SharePoint 2010 that recuperates the latest page of a certain (custom) type, according to publishing date. It only takes into account pages tagged with a specified term. I would like it to be able to also do so with pages that are tagged with terms which are children of the selected terms.

    If I have a term tree like so:

    • England
      • Kent
        • Dover
        • Canterbury
      • Surrey
        • Croydon
        • Crawley

    then by selecting Kent, I want my webpart to show the latest page tagged with Kent, Dover, or Canterbury.

    Is this possible in C# ?

    Thanks for your time.

  • Tom Resing
    Tom Resing about 13 years
    GetTerms is key to the answer. CAML is great for one list or a Content Query Webpart. See my updated answer for how to get your initial term guid and a method to use search across all lists and libraries.