More in this section

Forums / Developing with Sitefinity / Filtering TaxonomyControl

Filtering TaxonomyControl

6 posts, 1 answered
  1. Richard Baugh
    Richard Baugh avatar
    201 posts
    Registered:
    22 Aug 2012
    23 Jun 2011
    Link to this post
    I was wanting to use the TaxonomyControl to display the categories for the news, but I was wanting to only show the categories at a certain node. Is there a way for me to set the TaxonomyControl to use a child node for the Category taxonomies as the starting point?

    So you have this break down for categories:
    Cat 1
    Cat 2
        Cat sub 1
        Cat sub 2
        Cat sub 3
            Cat child sub 1
            Cat child sub 2

    In this scenario, I would like to use the TaxonomyControl to display only the taxonomies starting at the node "Cat Sub 3". I know how to get the Id (GUID) for the node, but not sure how I could go about filtering the control.

    Edit::
    I am not sure if this ever got built into the latest build, so I would like to know if this is/will be possible with the current or future release? Till then, I am hoping that the information from this post will help me achieve what I am looking for.
    http://www.sitefinity.com/devnet/forums/sitefinity-4-x/general-discussions/how-to-use-classifications-gt-categories-widget.aspx
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    24 Jun 2011
    Link to this post
    Hello Richard,

    Here is  a sample code that inherits from the base TaxonomyControl and shows how to filter by a certain tag which ID you should pass the the exposed public property

    Copy Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Telerik.Sitefinity.Web.UI.PublicControls;
    using Telerik.Sitefinity.Web.UI;
    using Telerik.Sitefinity.Taxonomies.Model;
    using Telerik.Sitefinity.Lists.Model;
     
    namespace Telerik.Sitefinity.Samples1
    {
        public class TaxonomyControlCustom : TaxonomyControl
        {
     
            protected override string LayoutTemplateName
            {
                get
                {
                    return null;
                }
            }
     
            public override string FieldName
            {
                get
                {
                    return "Category";
                }
               
            }
     
            public override Guid TaxonomyId
            {
                get
                {
                    return  new Guid("e5cd6d69-1543-427b-ad62-688a99f5e7d4");
                }
            }
     
            public Guid RootTaxon
            {
                get
                {
                    return this._rootTaxon;
                }
     
                set
                {
                    this._rootTaxon = value;
                }
            }
     
     
            public override string LayoutTemplatePath
            {
                get
                {
                    return TaxonomyControlCustom.layoutTemplatePath;
                }
            }
     
            protected override void InitializeTaxaList()
            {
                Dictionary<ITaxon, uint> taxonCountMap = null;
                if (this.ContentId != Guid.Empty)
                {
                    taxonCountMap = this.GetTaxaFromContentItem();
                }
                else
                {
                    taxonCountMap = this.GetTaxaItemsCountForTaxonomy();
     
                    var taxonomyId = this.TaxonomyId;
                    var stats =
                        this.CurrentTaxonomyManager.GetStatistics().Where(
                                                                          t =>
                                                                          t.TaxonomyId == taxonomyId &&
                                                                          t.MarkedItemsCount > 0 &&
                                                                          t.StatisticType == GenericContent.Model.ContentLifecycleStatus.Live);
                    if (this.ContentType != null)
                    {
                        var typeName = this.ContentType.FullName;
                        stats = stats.Where(t => t.DataItemType == typeName);
                    }
                     
                    var count = stats.Count();
                     
                    if (count > this.TaxaCount && this.TaxaCount > 0)
                    {
                        this.SeeAllTaxaLink.Visible = true;
                        this.SeeAllTaxaLink.Text = string.Format(this.SeeAllTextFormat, count, this.Taxonomy.Title.ToLower());
                        this.SeeAllTaxaLink.NavigateUrl = this.BaseUrl;
                    }
                }
     
     
                var singleTaxon = taxonCountMap.Single(kvp => ((HierarchicalTaxon)kvp.Key).Id == RootTaxon);
                IList<HierarchicalTaxon> items = null;
                    if (((Telerik.Sitefinity.Taxonomies.Model.HierarchicalTaxon)(singleTaxon.Key)).Subtaxa.Count > 0)
                    {
                        items = ((Telerik.Sitefinity.Taxonomies.Model.HierarchicalTaxon)(singleTaxon.Key)).Subtaxa;
                        items.Add((Telerik.Sitefinity.Taxonomies.Model.HierarchicalTaxon)(singleTaxon.Key));
                    }
                    else
                    {
                        items.Add((Telerik.Sitefinity.Taxonomies.Model.HierarchicalTaxon)(singleTaxon.Key));
                    }
     
                    
                List<TaxonData> data = BinderTaxonDataResult(items);
                if (data != null)
                {
     
                    this.BindRepeater(data);
                }
                this.IsEmpty = (data.Count == 0);
            }
            protected override void BindRepeater(List<TaxonData> data)
            {
     
                this.TaxaRepeater.DataSource = data;
                this.TaxaRepeater.ItemDataBound += base.TaxaRepeater_ItemDataBound;
                this.TaxaRepeater.DataBind();
            }
     
            protected virtual List<TaxonData> BinderTaxonDataResult(IList<HierarchicalTaxon> items)
            {
                List<TaxonData> data = new List<TaxonData>();
                foreach (HierarchicalTaxon t in items)
                {
                    TaxonData d = new TaxonData();
                    d.Title = t.Title;
                    d.Url = t.UrlName;
                    d.Size = 1;
                    d.Count = 1;
                    data.Add(d);
                }
     
                if (data.Count > 0)
                    return data;
                return null;
            }
     
            
     
            public static readonly string layoutTemplatePath =
                ControlUtilities.ToVppPath("Telerik.Sitefinity.Resources.Templates.PublicControls.TaxonomyControl.ascx");
     
            private Guid _rootTaxon;
        }
    }


    Here is the way that this could be implemented in a user control or custom control that uses RadTreeView.

    Copy Code
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RadTreeCategoryControl.ascx.cs" Inherits="SitefinityWebApp.Controls.RadTreeCategoryControl" %>
     
     
    <telerik:RadTreeView runat="server"  ID="RadTreeView1"></telerik:RadTreeView>

    Copy Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Sitefinity.Taxonomies.Model;
    using Telerik.Sitefinity.Taxonomies;
     
    namespace SitefinityWebApp.Controls
    {
        public partial class RadTreeCategoryControl : System.Web.UI.UserControl
        {
     
            public  string FieldName
            {
                get
                {
                    return "Category";
                }
     
            }
     
            public  Guid TaxonomyId
            {
                get
                {
                    return new Guid("e5cd6d69-1543-427b-ad62-688a99f5e7d4");
                }
            }
     
            public Guid RootTaxon
            {
                get
                {
                    return this._rootTaxon;
                }
     
                set
                {
                    this._rootTaxon = value;
                }
            }
     
            protected void Page_Load(object sender, EventArgs e)
            {
     
                var tManager = TaxonomyManager.GetManager();
                ITaxonomy taxonomy = tManager.GetTaxonomy(TaxonomyId);
     
                var singleTaxon = taxonomy.Taxa.Single(st => ((HierarchicalTaxon)st).Id == RootTaxon);
                IList<HierarchicalTaxon> items = null;
     
                if (((Telerik.Sitefinity.Taxonomies.Model.HierarchicalTaxon)(singleTaxon)).Subtaxa.Count > 0)
                {
                    items = ((Telerik.Sitefinity.Taxonomies.Model.HierarchicalTaxon)(singleTaxon)).Subtaxa;
                    items.Add((Telerik.Sitefinity.Taxonomies.Model.HierarchicalTaxon)(singleTaxon));
                }
                else
                {
                    items.Add((Telerik.Sitefinity.Taxonomies.Model.HierarchicalTaxon)(singleTaxon));
                }
     
                this.BindTree(items);
     
            }
     
            protected void BindTree(IList<HierarchicalTaxon> data)
            {
     
                this.RadTreeView1.DataSource = data;
                this.RadTreeView1.NodeDataBound += new Telerik.Web.UI.RadTreeViewEventHandler(RadTreeView1_NodeDataBound);
                this.RadTreeView1.DataBind();
            }
     
            void RadTreeView1_NodeDataBound(object sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
            {
                var taxon = e.Node.DataItem as HierarchicalTaxon;
                e.Node.Text = taxon.Title;
            }
     
           private Guid _rootTaxon;
     
        }
    }

    You need to format the URL of the category so that it is

    /-in-Category/Categories/  and after that goes the cateogry name

    example
    /-in-Category/Categories/rootcategory
    /-in-Category/Categories/rootcategory/child1
    /-in-Category/Categories/rootcategory/child1/nestedchild


    Greetings,
    Ivan Dimitrov
    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
    Answered
  3. Richard Baugh
    Richard Baugh avatar
    201 posts
    Registered:
    22 Aug 2012
    24 Jun 2011
    Link to this post
    Thank you Ivan. Will this be implemented in a later release?
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    24 Jun 2011
    Link to this post
    Hello Richard ,

    I remember that there is a task about this, but it is currently not scheduled.  Here you can find the PITS Issue: Public URL

    Greetings,
    Ivan Dimitrov
    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
  5. Richard Baugh
    Richard Baugh avatar
    201 posts
    Registered:
    22 Aug 2012
    24 Jun 2011
    Link to this post
    Ivan,
    I did notice that they count is always 1 if you set the ShowItemCount. I believe this is due to this line:
    d.Count = 1;

    How would I get this to output the actual item count for each taxon?
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    27 Jun 2011
    Link to this post
    Hi Richard,

    You can use PrepareData method of the TaxonomyControl to do this, but in this case you should override it to split the taxons. Another option is using GetItemsByTaxon method of ContentDataProviderBase and count the result.

    Regards,
    Ivan Dimitrov
    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
6 posts, 1 answered