Display Category Name Only - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Display Category Name Only

  • Display Category Name Only
  • New to Telerik and .Net in general but I have a couple of questions I'm hoping to get some help on. I'm trying to use the category name of an event as part of a URL that I'm trying to structure within a custom widget.


    www.url.com/.../category name

    I have spent a lot of time looking for the answer online of how to simply display the category name but have only come up with the following code:

    <sitefinity:HierarchicalTaxonField ID="HierarchicalFieldControl" runat="server" TaxonomyId="E5CD6D69-1543-427b-AD62-688A99F5E7D4" DisplayMode="Read" WebServiceUrl="~/Sitefinity/Services/Taxonomies/HierarchicalTaxon.svc" Expanded="false" TaxonomyMetafieldName="Category" BindOnServer="true" />

    And while it does display the category name it also displays a bunch of html along with it that I don't want. I simply want a string of the name itself.

    While I have some experience with c# and .Net I don't have any experience doing any kind of code behind in Sitefinity or custom whatevers and have been writing any c# inline within the widgets and has worked well for now. If I'm able to do that with displaying the category name it would be most helpful.

  • Hi Jesse,

    Can you please share some additional information with us regarding where you'd like to display the category names so we can make sure we're addressing the same issue.

    Do you simply need to display a list of each category that's assigned to an item in the list/details view of one of our ContentView widgets (e.g. Blogs) or do you need to have filtering ability for these names as well?

    By default the Hierarchical(or Flat)taxonField controls will resolve the taxa an item has been marked with, and display their titles if the control is placed on the widget template.

    However currently the Taxon filed controls used on widget templates do not output click-able links, but just plain text for the categories and tags assigned to the item. This is a feature that has not yet been implemented in the product, you can track the feature status and vote for its popularity in PITS here.

    A sample workaround approach that can be implemented in the code behind of an external widget template is to extend the default template for blog posts and add a placeholder that would rener the taxonomy links for each item (in the ItemTemplate of the RadListView control displaying the blog posts). Then, in the external template's codebehind you can subscribe to the RadListView's ItemDataBound event and for each item that passes through there. process the assigned categories and render links. For example:

    Copy Code
    protected void Page_Load(object sender, EventArgs e)
              NewsList.ItemDataBound += new EventHandler<Telerik.Web.UI.RadListViewItemEventArgs>(NewsList_ItemDataBound);
          void NewsList_ItemDataBound(object sender, Telerik.Web.UI.RadListViewItemEventArgs e)
                  case RadListViewItemType.DataItem:
                  case RadListViewItemType.AlternatingItem:
                      var listViewDataItem = (RadListViewDataItem)e.Item;
                      var dataItem = (NewsItem)listViewDataItem.DataItem;
                      if (dataItem != null)
                          var placeholder = e.Item.FindControl("CategoriesLabel") as SitefinityLabel;
                          placeholder.Text = RenderHierarchicalTaxaAsLink(dataItem, "Category");
    We subscribe to the ItemDataBound event of the RadListView. You will have to do the same thing but subscribe for the RadListView of the List items widget template. Then when each item is bound we check whether it is a dataItem ,we get the item and the dataItem (which contains all properties of the items). In the template we have added a label, which displays our taxonomy items:

    Copy Code
    Copy Code
    var placeholder = e.Item.FindControl("CategoriesLabel") as SitefinityLabel;
    Then we call our RenderHierarchicalTaxaAsLink method (created by us). Here's the method and what it does:
    Copy Code
    Copy Code
    public string RenderHierarchicalTaxaAsLink(NewsItem newsitem, string taxonomyFieldName)
               var catList = "";
               var categories = newsitem.GetValue<TrackedList<Guid>>(taxonomyFieldName);
               if (categories != null && categories.Count() > 0)
                   catList = "Categories:</br> ";
                   var currentUrl = HttpContext.Current.Request.Url.ToString();
                   currentUrl = currentUrl.TrimEnd('/');
                   var taxManager = TaxonomyManager.GetManager();
                   foreach (var cat in categories)
                       var t = taxManager.GetTaxon<HierarchicalTaxon>(cat);
                       var url = string.Format("0/-in-1/2/3",currentUrl, t.Taxonomy.TaxonName, t.Taxonomy.Name, t.Name);
                       var link = string.Format( "<a href='0'>1</a><br/>", url, t.Name).ToString();
                       catList = catList + link;

    It gets the taxonomy item, which is assigned to the list item and we construct the same link, which the Tags widget generates. For example:

    I hope you find the above information useful.
    Please do not hesitate to let us know if you need some additional information, or the desired use case scenario differs from the one discussed above.

    Boyan Barnev
    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
  • yeah, this seems to be more work than its worth. All i want to do is to convert a single tag that would be attached to an item and display that tag as a string so I can have it be part of a URL like so:

    www.url.com/.../category name

    I appreciate the help though. If you know of an easier way to accomplish this outside of getting into the code behind I'd appreciate hearing it(or in this case reading it)

  • Jesse, did you ever find a solution to this?  I'm trying to figure out the same thing and I came across this post.