Custom TaxonomyControl to Custom BlogPostMasterView

Posted by Community Admin on 04-Aug-2018 23:14

Custom TaxonomyControl to Custom BlogPostMasterView

All Replies

Posted by Community Admin on 12-Feb-2013 00:00

We have the template of  TaxonomyControl customized because we want the tags list to be rendered as links. Our plan is to place this control inside our custom BlogPostMasterView to act as a list of tags per blog post. Our TaxonomyControl and BlogPostMasterView lives in the same class library with the following namespaces:

WebsiteControls.Controls.Blogs.BlogPostMasterView

WebsiteControls.Controls.Generic.TaxonomyControl

We noticed that if we registrer the TaxonomyControl to the BlogPostMasterView the control returns an error "Error parsing the template". The line of code we for registering the control is show below:

<%@ Register TagPrefix="wc" Namespace="WebsiteControls.Controls.Generic" TagName="TaxonomyControl"  Assembly="WebsiteControls" %> 

Also this is the code inside the TaxonomyControl.cs:

[RequireScriptManager(true)]
    public class TaxonomyControl : Telerik.Sitefinity.Web.UI.PublicControls.TaxonomyControl
   
        private const string _layoutTemplatePath = "WebsiteControls.Controls.Generic.TaxonomyControl.TaxonomyControl.ascx";
        public override string LayoutTemplatePath
       
            get
           
                return ConfigurationManager.AppSettings.Get("WebsiteControls_VirtualPath") + _layoutTemplatePath;
           
       
   

Whats wrong in here?


Posted by Community Admin on 15-Feb-2013 00:00

Hi,

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:

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)
      
          switch(e.Item.ItemType)
          
              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");
                  
              break;
          
  
      
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
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
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();
               if(currentUrl.EndsWith("/"))
               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:
localhost:60876/.../testTag


Please do not hesitate to let me know if you need some additional information, it was a pleasure for me to assist you.



Greetings,
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

Posted by Community Admin on 15-Feb-2013 00:00

Thank you for your reply. At the moment we decided to just make our own control. We appreciate your help and we will keep your response for future references.

This thread is closed