1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Custom TaxonomyControl to Custom BlogPostMasterView

Custom TaxonomyControl to Custom BlogPostMasterView

3 posts, 0 answered
  1. Ernest
    Ernest avatar
    37 posts
    Registered:
    12 Mar 2012
    12 Feb 2013
    Link to this post

    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?


  2. Boyan Barnev
    Boyan Barnev avatar
    1429 posts
    Registered:
    28 Aug 2017
    15 Feb 2013
    Link to this post
    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:
    http://localhost:60876/testpage1/-in-Tags/Tags/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
  3. Ernest
    Ernest avatar
    37 posts
    Registered:
    12 Mar 2012
    15 Feb 2013 in reply to Boyan Barnev
    Link to this post

    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.

3 posts, 0 answered