More in this section

Forums / Sitefinity SDK / Query Categories (HierarchicalTaxonomies)

Query Categories (HierarchicalTaxonomies)

8 posts, 0 answered
  1. Duneel
    Duneel avatar
    166 posts
    Registered:
    08 Dec 2010
    06 Apr 2011
    Link to this post
    Hi,

    Can someone please tell me how to query the categories under a certain category using FluentAp?. Let say i have created the categories as folows in Sitefinity. And I have the GUID for the "Parent Category 2". So what is the FluentAPI query I need to have to get the list of categories under "Parent category 2"

    Parent category 1
        - Category 1
        - Category 2
        - Category 3
    Parent category 2
        - Category 4
        - Category 5
    Parent category 3
        - Category 6
        - Category 7

    Thanks,
    Duneel
  2. Duneel
    Duneel avatar
    166 posts
    Registered:
    08 Dec 2010
    07 Apr 2011
    Link to this post

    Found the answer. Here you go....

    Guid gParent = new Guid(parentCategoryID);
    var taxonManager = TaxonomyManager.GetManager();
      
    IList<HierarchicalTaxon> subCategories = taxonManager.GetTaxa<HierarchicalTaxon>().Where(t => t.Parent.Id == gParent).ToList();
  3. OC
    OC avatar
    129 posts
    Registered:
    17 Nov 2004
    23 Nov 2011
    Link to this post
    I'm having a real struggle with this. No matter what I do I get all taxons, one level taxons or no taxons at all.

    I want to bind a RadPanelBar to one department with all sub-categories.

    I do not see why this should not work:

    SiteMapNode node = SiteMapBase.GetCurrentProvider().CurrentNode;
     
    TaxonomyManager tm = TaxonomyManager.GetManager();
    var departments = tm.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name == "Departments").SingleOrDefault();
    var currentDepartment = departments.Taxa.Where(t => t.Name == currentDepartmentName(node.Url)).SingleOrDefault();
    IList<HierarchicalTaxon> categories = tm.GetTaxa<HierarchicalTaxon>().Where(t => t.Parent.Id == currentDepartment.Id).ToList();
     
    PanelBar1.DataSource = categories;
    PanelBar1.DataBind();

    Anyone?

    OC
  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    25 Nov 2011
    Link to this post
    Hi Ole,

    Please consider bellow sample code which show how to get all sub taxons of a parent taxon.
    public partial class HierarchicalTaxonHolder
    {
        public HierarchicalTaxonHolder(HierarchicalTaxon taxon)
        {
            this.taxon = taxon;
        }
        public Guid ParentId
        {
            get
            {
                if (this.Parent == null)
                    return Guid.Empty;
                return this.Parent.Id;
            }
        }
     
        public HierarchicalTaxon Parent
        {
            get
            {
                return this.Taxon.Parent;
            }  
        }
     
        public Guid Id
        {
            get
            {
                return this.Taxon.Id;
            }
        }
     
        public string Title
        {
            get
            {
                return this.Taxon.Title;
            }
        }
        public HierarchicalTaxon Taxon
        {
            get
            {
                return this.taxon;
            }
        }
     
        private HierarchicalTaxon taxon;
    }
     
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btn1.Click += new EventHandler(btn1_Click);
        TaxonomyManager taxManager = TaxonomyManager.GetManager();
        HierarchicalTaxonomy departmentsTaxonomy = taxManager.GetTaxonomy<HierarchicalTaxonomy>(TaxonomyManager.DepartmentsTaxonomyId);
        var parentDepartment = taxManager.GetTaxa<HierarchicalTaxon>().Where(dT => dT.Taxonomy == departmentsTaxonomy && dT.Title == "IT").FirstOrDefault();
        //construct DS
        List<HierarchicalTaxonHolder> ds = new List<HierarchicalTaxonHolder>();
        ds.Add(new HierarchicalTaxonHolder(parentDepartment));
        ConstructDataSource(parentDepartment, ds);
     
        this.PanelBar1.DataSource = ds;
        this.PanelBar1.DataFieldParentID = "ParentId";
        this.PanelBar1.DataFieldID = "Id";
        this.PanelBar1.DataTextField = "Title";
        this.PanelBar1.DataBind();
    }
     
    public void ConstructDataSource(HierarchicalTaxon parent, List<HierarchicalTaxonHolder> items)
    {
        if (parent.Subtaxa.Count > 0)
        {
            foreach (var subtaxa in parent.Subtaxa)
            {
                HierarchicalTaxonHolder holder = new HierarchicalTaxonHolder(subtaxa);
                items.Add(holder);
                ConstructDataSource(subtaxa, items);
            }
        }
        return;
    }


    Kind regards,
    Radoslav Georgiev
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  5. Chaitanya
    Chaitanya avatar
    22 posts
    Registered:
    14 Oct 2013
    16 May in reply to Duneel
    Link to this post
    Duneel's answer works fine. I have a question though. Even I move taxonomies up and down in backend the order in which  I get items in code do not change. Is there any issue or its expected behavior?
  6. Victor Leontyev
    Victor Leontyev avatar
    65 posts
    Registered:
    01 Jul 2014
    17 May in reply to Chaitanya
    Link to this post

    Taxons has field Ordinal. When you move taxonomies up and down, sitefinity is changing this field.  Example how you can receive sorted list by this field:

    taxManager.GetTaxa<HierarchicalTaxon>().OrderBy(i=>i.Ordinal); //order asc
    taxManager.GetTaxa<HierarchicalTaxon>().OrderByDescending(i=>i.Ordinal); //order desc

     

     

  7. Chaitanya
    Chaitanya avatar
    22 posts
    Registered:
    14 Oct 2013
    17 May in reply to Victor Leontyev
    Link to this post

    Thanks Victor,

    That was helpful.

  8. Chaitanya
    Chaitanya avatar
    22 posts
    Registered:
    14 Oct 2013
    17 May in reply to Victor Leontyev
    Link to this post

    Thanks Victor,

    That was helpful.

8 posts, 0 answered