0

I need to Iterate the terms using Termset ID, I Tried

TaxonomySession taxonomySession = new TaxonomySession(site);
                        TermStore termstore = taxonomySession.DefaultSiteCollectionTermStore;

                        TermSet breadcrumbTermSet = termstore.GetTermSet(new Guid("fdc98309-c520-44eb-a5ca-f2a04d8dd541"));
                        var terms = breadcrumbTermSet.Terms;
                        var siteCollectionURL = SPContext.Current.Web.Url;
                        var requestURL = HttpContext.Current.Request.Url;
                        string termSetName = "", termName = "";

                        foreach (var parentTerm in terms)
                        {
                            if (parentTerm.LocalCustomProperties["_Sys_Nav_SimpleLinkUrl"] == siteCollectionURL.ToString())
                            {
                                termSetName = " / &nbsp" + parentTerm.Name;
                            }
                            foreach (var childTerm in parentTerm.Terms)
                            {
                                if (parentTerm.LocalCustomProperties["_Sys_Nav_SimpleLinkUrl"] == requestURL.ToString())
                                {
                                    termSetName = " / &nbsp" + childTerm.Name;
                                }
                            }
                        }

But I can't, I need more Reference about this, can you please suggest some Idea to achieve this

1
  • Can you let me know the error you are getting?
    – Amit
    Commented Nov 28, 2016 at 9:56

2 Answers 2

1

In C# Server object model, you can try it as below:

TaxonomySession taxonomySession = new TaxonomySession(site);
TermStore termstore = taxonomySession.DefaultSiteCollectionTermStore;

Guid termSetGuid = new Guid("fdc98309-c520-44eb-a5ca-f2a04d8dd541");

TermSet breadcrumbTermSet = termstore.GetTermSet(termSetGuid);
TermCollection terms = breadcrumbTermSet.GetAllTerms();
var siteCollectionURL = SPContext.Current.Web.Url;
var requestURL = HttpContext.Current.Request.Url;
string termSetName = "", termName = "";

foreach (var parentTerm in terms)
{
    if (parentTerm.LocalCustomProperties["_Sys_Nav_SimpleLinkUrl"] == siteCollectionURL.ToString())
    {
        termSetName = " / &nbsp" + parentTerm.Name;
    }
    foreach (var childTerm in parentTerm.Terms)
    {
        if (parentTerm.LocalCustomProperties["_Sys_Nav_SimpleLinkUrl"] == requestURL.ToString())
        {
            termSetName = " / &nbsp" + childTerm.Name;
        }
    }
}
2

There are couple options you can try.

  1. You can call TermSet.GetAllTerms() method to return all terms under that TermSet.
  2. Another approach you can try is to use recursive function to enumerate termSet.Terms and term.Terms

I list the code for approach 1 at the following. You can check term.PathOfTerm to find out the term's hierarchy information.

    class Program
    {
    static void Main(string[] args)
    {
        Test();
    }
    private static void Test()
    {
        string url = "http://sp1:1111";
        using (ClientContext context = new ClientContext(url))
        {
            TaxonomySession session = TaxonomySession.GetTaxonomySession(context);
            TermStore store = session.GetDefaultSiteCollectionTermStore();
            TermSet termSet = store.GetTermSet(new Guid("6e17ec6e-7456-4e61-82e9-d5a75f6932fa"));
            TermCollection terms = termSet.GetAllTerms();
            context.Load(terms);
            context.ExecuteQuery();
            foreach(Term term in terms)
            {
                Console.WriteLine(term.PathOfTerm);
            }
            Console.WriteLine();
        }
    }
}

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.