Using Categories control to limit shown categories

Posted by Community Admin on 05-Aug-2018 09:32

Using Categories control to limit shown categories

All Replies

Posted by Community Admin on 07-May-2013 00:00

Hi

I have a categories control on a page. By default it shows all categories. I would like to limit it to showing only the sub-categories contained in a specific category. How can I do this?

thanks,
Dan

Posted by Community Admin on 10-May-2013 00:00

Hello Dan,

Thank you for using our services.

Here's how to create a sub-category category widget:

1. First we add a class file to our Sitefinity project and name it CustomTaxonomyControl. Our class inherits from TaxonomyControl. The TaxonomyControl has several modes in which it renders taxonomies - HorizontalList, VerticalList and Cloud. Each one of the options is represented by a Repeater on the front-end. We override InitializeControls method of TaxonomyControl and subscribe to the TaxaRepeater ItemDataBound event. TaxaRepeater is the repeater, which renders our taxonomies. Then we can the base implementation of InitializeControls:
 

protected override void InitializeControls(Telerik.Sitefinity.Web.UI.GenericContainer container)
      
          this.TaxaRepeater.ItemDataBound -= base.TaxaRepeater_ItemDataBound;
          this.TaxaRepeater.ItemDataBound += TaxaRepeater_ItemDataBound123;
          base.InitializeControls(container);
  
      
In our custom control we have three properties - ParentCategory, FieldName and ParentCategory. The first two come from TaxonomyControl and we hardcode their values, so that the field name will always be Category (Tags do not have hierarchical structure), and the taxonomyId will be set to the Categories taxonomy id. We do that in case we're going to use the control to display categories only. If you want to use it for other hierarchical taxonomies, you don't need to fill these properties. You will be able to fill them though the Advanced properties of the widget (in the UI) and set the fieldName to your hierarchical taxonomy's fieldName, as well as the TaxonomyId to its corresponding id. The ParentCategory property from type string is added by us, and we'll use it to specify the parent (root category). By doing this, your widget will only display categories (hierarchical taxonomies, which are children of this taxonomy).

In the ItemDataBound event of the Repeater control we call the base ItemDataBound  and then we check the url of each taxonomy, bound to the Repeater. If the url doesn't contain our parentCategory, followed by forward slash (this way we make sure that this will catch the parent taxonomy, as well), we set Visible = false for these items. 
protected internal virtual void TaxaRepeater_ItemDataBound123(object sender, RepeaterItemEventArgs e)
    base.TaxaRepeater_ItemDataBound(sender, e);
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    
        var taxonData = e.Item.DataItem as TaxonData;
        var link = e.Item.FindControl("link") as HyperLink;
  
        if (link != null)
        
            if (ParentCategory!=null && !taxonData.Url.Contains(ParentCategory.Trim().ToLower() + "/"))
            
                e.Item.Visible = false;
            
  
        
    
You can register the control as a custom control in Sitefinity and use it.

The whole code is attached to the ticket. All the best,
Jen Peleva
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 28-Sep-2014 00:00

Hi, I'm trying to put this to work.

I got the control registered and I can add it to a page.

When I add the parent ParentCategory all the categories disappear. No errors, in the code only the ul gets printed.

I'm sure the id of the ParentCategory is correct, what am I missing?

 

Posted by Community Admin on 01-Oct-2014 00:00

Hello Filippo,

Could you please provide your control in order to inspect the code or open a support ticket on that matter.

In addition you can take a look on the Taxonomies API documentation.

Regards,
Svetoslav Manchev
Telerik

 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

This thread is closed