Clean Sitefinity Taxonomy Widget, you should use this

September 15, 2012
The current sitefinity Taxon controls are...extreme.  So if you link a content item to one single category, and you have 5000 categories in your system, the client renders out all 5000 into a hidden control on the page (for some reason)...serves to do nothing but slow down everything b/c your DOM is massive (jQuery parsing, markup size from the server, etc).

It's been an open issue for over a year, no fix in just yet, so until then here's a replacement that just renders out the tags nice and clean (with a seperator)...modify as desired :)

(Assumes the control lives here ~/Usercontrols/Widgets/Taxonomy/SimpleTaxa/SimpleTaxa.ascx)

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;
using Telerik.Sitefinity.DynamicModules;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Taxonomies;
using Telerik.Sitefinity.Taxonomies.Model;
using Telerik.Sitefinity.Web.UI;
 
namespace SitefinityWebApp.Usercontrols.Widgets.Taxonomy.SimpleTaxa {
    public class SimpleTaxa : SimpleView {
 
        protected override void InitializeControls(GenericContainer container) {
 
            if(!String.IsNullOrEmpty(this.Title)){
                titleLabel.Text = String.Format("<{0} class='sfitemFieldLbl taxa-title'>{1}</{0}>", this.TitleWrapperTag, this.Title);
            }
 
            //Set the type to show, hardcoded to category and tags
            if (this.TaxonomyType == "Category" || this.TaxonomyType == "Categories")
            {
                this.TaxomonyMetafieldName = "Category";
                this.TaxonomyID = new Guid("e5cd6d69-1543-427b-ad62-688a99f5e7d4");
            }
            else if (this.TaxonomyType == "Tags" || this.TaxonomyType == "Tag")
            {
                this.TaxomonyMetafieldName = "Tags";
                this.TaxonomyID = new Guid("cb0f3a19-a211-48a7-88ec-77495c0f5374");
            }
 
            //Set the wrapper
            wrapperLiteral.Text = String.Format("<div class='taxonomy-wrapper {0}'>", this.TaxomonyMetafieldName.ToLower());
 
            //Get the data item and it's taxa
            object dataItem = null;
            IDataItemContainer parent = this.Parent as IDataItemContainer;
            if (parent != null) {
                dataItem = (IDataItem)parent.DataItem;
 
                if (dataItem != null) {
                    if (!String.IsNullOrEmpty(this.TaxomonyMetafieldName)) {
                        TaxonomyPropertyDescriptor property = OrganizerBase.GetProperty(dataItem.GetType(), this.TaxomonyMetafieldName) as TaxonomyPropertyDescriptor;
                        if (property != null) {
                            IList<Guid> @value = property.GetValue(dataItem) as IList<Guid>;
                            TaxonomyManager manager = TaxonomyManager.GetManager();
                            List<ITaxon> taxa = new List<ITaxon>();
                            foreach (Guid guid in @value) {
                                taxa.Add(manager.GetTaxon(guid));
                            }
                             
                            if (taxa != null && taxa.Count > 0) {
                                //DATABIND
                                TaxaRepeater.DataSource = taxa;
                                TaxaRepeater.DataBind();
                            }
 
                            if (taxa.Count == 0 && this.HideWhenNoTaxaFound) {
                                this.Visible = false;
                            }
                        }
                    }
                }
            }
        }
 
 
 
        #region PROPERTIES
         
        private Guid _taxonomyID = Guid.Empty;
        private Guid TaxonomyID {
            get { return _taxonomyID; }
            set { _taxonomyID = value; }
        }
 
        public string Title { get; set; }
         
        private string _taxonomyMetaFieldName = String.Empty;       
        private string TaxomonyMetafieldName {
            get { return _taxonomyMetaFieldName; }
            set { _taxonomyMetaFieldName = value; }
        }
         
        private bool _hideWhenNoTaxaFound = true;
        public bool HideWhenNoTaxaFound {
            get { return _hideWhenNoTaxaFound; }
            set { _hideWhenNoTaxaFound = value; }
        }
 
        private string _type = "Category";
        public string TaxonomyType {
            get { return _type; }
            set { _type = value; }
        }
 
        private string _titleWrapperTag = "h4";
        public string TitleWrapperTag {
            get { return _titleWrapperTag; }
            set { _titleWrapperTag = value; }
        }
 
        #endregion
         
        #region LAYOUTS
         
        /// <summary>
        /// Gets the layout template path
        /// </summary>
        public override string LayoutTemplatePath {
            get { return SimpleTaxa.layoutTemplatePath; }
        }
         
        /// <summary>
        /// Gets the layout template name
        /// </summary>
        protected override string LayoutTemplateName {
            get { return String.Empty; }
        }
 
        #endregion
         
        #region Control References
        protected virtual Repeater TaxaRepeater {
            get { return this.Container.GetControl<Repeater>("taxaRepeater", true); }
        }
 
        protected virtual Literal titleLabel
        {
            get { return this.Container.GetControl<Literal>("titleLabel", true); }
        }
         
        protected virtual Literal wrapperLiteral {
            get { return this.Container.GetControl<Literal>("wrapperLiteral", true); }
        }
         
        #endregion
         
        #region METHODS
        /// <summary>
        /// Remove the outer span on the control
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            RenderContents(writer);
        }
 
        #endregion
         
        #region Private members & constants
 
        public static readonly string layoutTemplatePath = "~/Usercontrols/Widgets/Taxonomy/SimpleTaxa/SimpleTaxa.ascx";
 
        #endregion
 
    }
}


Markup

<%@ Control Language="C#" %>
 
<asp:Literal ID="wrapperLiteral" runat="server" />
    <asp:Literal ID="titleLabel" runat="server" />
    <asp:Repeater ID="taxaRepeater" runat="server" EnableViewState="false">
        <HeaderTemplate>
            <div class="taxa-list">
        </HeaderTemplate>
        <ItemTemplate>
            <span class="taxa">
                <%# ((Telerik.Sitefinity.Taxonomies.Model.ITaxon)Container.DataItem).Title %>
            </span>
        </ItemTemplate>
        <SeparatorTemplate>
            <span class='seperator'>, </span>
        </SeparatorTemplate>
        <FooterTemplate>
            </div>
        </FooterTemplate>
    </asp:Repeater>
</div>

Using it in a template

Find and replace these:
<div class='sfitemHierarchicalTaxon sfitemTaxonWrp'>       
                <sf:SitefinityLabel runat="server" Text='Categories:' WrapperTagName="div" HideIfNoText="true" CssClass="sfitemFieldLbl" />
                <sf:HierarchicalTaxonField runat="server" DisplayMode="Read" WebServiceUrl="~/Sitefinity/Services/Taxonomies/HierarchicalTaxon.svc" TaxonomyId="e5cd6d69-1543-427b-ad62-688a99f5e7d4" Expanded="false" TaxonomyMetafieldName="Category" BindOnServer="true" />
            </div>
            <div class='sfitemFlatTaxon sfitemTaxonWrp'>       
                <sf:SitefinityLabel runat="server" Text='Tags:' WrapperTagName="div" HideIfNoText="true" CssClass="sfitemFieldLbl" />
                <sf:FlatTaxonField runat="server" DisplayMode="Read" WebServiceUrl="~/Sitefinity/Services/Taxonomies/FlatTaxon.svc" TaxonomyId="cb0f3a19-a211-48a7-88ec-77495c0f5374" AllowMultipleSelection="true" TaxonomyMetafieldName="Tags" Expanded="false" BindOnServer="true" />
            </div>

...with this
<!-- Register it at the top of the template -->
<%@ Register Assembly="SitefinityWebApp" Namespace="SitefinityWebApp.Usercontrols.Widgets.Taxonomy.SimpleTaxa" TagPrefix="sf" %>

<sf:SimpleTaxa runat="server" TaxonomyType="Category" Title="Categories:" />
<sf:SimpleTaxa runat="server" TaxonomyType="Tags" Title="Tags:"  />

And the end result is just this...clean
<div class="taxonomy-wrapper category">
    <h4 class="sfitemFieldLbl taxa-title">Categories:</h4>
    <div class="taxa-list">
        <span class="taxa"> Ethnic, Unique Foods, Ingredients </span>
        <span class="seperator">, </span>
        <span class="taxa"> Coffee, Tea, Chocolate </span>
    </div>
</div>
<div class="taxonomy-wrapper tags">
    <h4 class="sfitemFieldLbl taxa-title">Tags:</h4>
    <div class="taxa-list">
        <span class="taxa"> featured </span>
    </div>
</div>

Again, tweak as needed

Steve


Image Description

Steve McNiven-Scott

Best\Only Blogger on this site. Therefore defacto blogger of the year Award Winner.