Query Categories (HierarchicalTaxonomies) - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Query Categories (HierarchicalTaxonomies)

  • Query Categories (HierarchicalTaxonomies)
  • 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
  • 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();
  • 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
  • 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 >>
  • 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?
  • 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

     

     

  • Thanks Victor,

    That was helpful.

  • Thanks Victor,

    That was helpful.