Try Now
More in this section

Forums / Designing & Styling / Using Categories control to limit shown categories

Using Categories control to limit shown categories

4 posts, 0 answered
  1. Dan
    Dan avatar
    8 posts
    30 Apr 2013
    07 May 2013
    Link to this post

    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?

  2. Jen Peleva
    Jen Peleva avatar
    568 posts
    10 Sep 2016
    10 May 2013
    Link to this post
    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;
    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
  3. Filippo
    Filippo avatar
    2 posts
    22 Sep 2014
    28 Sep 2014 in reply to Jen Peleva
    Link to this post

    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?


  4. Svetoslav Manchev
    Svetoslav Manchev avatar
    735 posts
    17 Oct 2016
    01 Oct 2014
    Link to this post
    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.

    Svetoslav Manchev
    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
4 posts, 0 answered