I need sample code where user can search terms from termstore by entering some term in textbox?

I am actually finding, is there any search function available for searching terms ? Like for finding users from UserProfileManager, we have search function as:

 UserProfileManager profileManager = new UserProfileManager(context);
                ProfileBase[] profiles = profileManager.Search(Request.QueryString["q"]);

We have profileManager.Search function as shown above, Similarly, I am wondering if this kind of search function is available for searching terms from termStore?

2 Answers 2


You can create a webpart which can search for collection of terms using this method,

Term.GetTerms Method (String, Int32, Boolean, StringMatchOption, Int32, Boolean)

MSDN Source

Here's some sample code,

 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;

Sample Code Source


What you could do is convert the list of your terms in something that is querable by LINQ and use it. For example:

    TaxonomyFieldValue v = null;
    TermCollection childTerms = null;
    var session = new TaxonomySession(site);
    if (session.TermStores != null && session.TermStores.Count > 0)
        var termStore = session.TermStores[0];
        var t = termStore.GetTerm(v.TermGuid);
        var parentTerm = t.Parent;   
        childTerms = t.GetTerms();

    if (childTerms.Count > 0)
      var res = childTerms.Cast(YourCustomClass).Where(w => w.Title.Equal("WhatISearch");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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