Query Categories (HierarchicalTaxonomies)

Posted by Community Admin on 05-Aug-2018 19:53

Query Categories (HierarchicalTaxonomies)

All Replies

Posted by Community Admin on 06-Apr-2011 00:00

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

Posted by Community Admin on 07-Apr-2011 00:00

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();

Posted by Community Admin on 23-Nov-2011 00:00

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

Posted by Community Admin on 25-Nov-2011 00:00

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 >>

Posted by Community Admin on 16-May-2017 00:00

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?

Posted by Community Admin on 17-May-2017 00:00

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

 

 

Posted by Community Admin on 17-May-2017 00:00

Thanks Victor,

That was helpful.

Posted by Community Admin on 17-May-2017 00:00

Thanks Victor,

That was helpful.

This thread is closed