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

Forums / Developing with Sitefinity / Define Order of News Categories

Define Order of News Categories

8 posts, 0 answered
  1. Jack
    Jack avatar
    10 posts
    Registered:
    31 Jul 2007
    08 Sep 2008
    Link to this post
    Hi, I'm on the latest version of Sitefinity. I want to know if there is any way of defining the order in which the news categories in the Categories List control display.

    When I create the categories it doesn't seem to do it in a logical order i.e. by dat created or alphabetically?

    Is there a way to achieve this?

    Thanks
  2. Jack
    Jack avatar
    10 posts
    Registered:
    31 Jul 2007
    08 Sep 2008
    Link to this post
    In addition - I'm having real problems getting the News to display in the categories.

    I have my Categories List control on a page, when a category is clicked it goes to the news page but all the news is there rather than just the news from that category.

    I presume some of the advanced settings need to be changed - I've tried various things with no success.
  3. Jack
    Jack avatar
    10 posts
    Registered:
    31 Jul 2007
    09 Sep 2008
    Link to this post
    **Update**

    Have fixed the second problem by setting the Behaviour Mode in the Advanced settings to Auto.

    Still having the problem of not being able to display the Categories in the order I want to.
  4. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    11 Sep 2008
    Link to this post
    Hello Jack,

    CategoriesList control does not provide an option for sorting the categories. But you could extend the control in order to achieve the desired functionality.

    Please refer to our Developer Manual and the following article: Finding Categories. You can sort by one of the ICategory Members. I think the most suitable method in your case is GetCategories(int from, int max, string sortExpr).

    Let us know if you have any other questions.

    Regards,
    Georgi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  5. Jack
    Jack avatar
    10 posts
    Registered:
    31 Jul 2007
    11 Sep 2008
    Link to this post
    Ok, so according to the manual I should be using the code below. Unfortunately I'm not very experienced in developing, could you tell we how to encorporate the code into my system.

    Thanks

    // create new instance of ContentManager
    Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
    // create a list of categories, from first ('0'), four by number ('4'), sorted by name
    IList listOfCategories = contentManager.GetCategories(0,4,"CategoryName ASC");
    if (listOfCategories.Count > 0)
    {
       
    foreach (Telerik.Cms.Engine.ICategory catItem in listOfCategories)
           Response.Write(catItem.CategoryName +
    "<br />");
    }
  6. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    12 Sep 2008
    Link to this post
    Hello Jack,

    You need to create a custom class that inherits CategoriesList base class and implement the required functionality like this:

    .CustomCategoriesList.cs
    using System;  
    using System.Data;  
    using System.Configuration;  
    using System.Web;  
    using System.Web.Security;  
    using System.Web.UI;  
    using System.Web.UI.HtmlControls;  
    using System.Web.UI.WebControls;  
    using System.Web.UI.WebControls.WebParts;  
    using Telerik.Cms.Engine.WebControls.Categories;  
    using Telerik.Cms.Engine;  
    using Telerik.Web;  
    using System.IO;  
     
    namespace Telerik.Cms.Engine.WebControls.Categories  
    {  
        public class CustomCategoriesList : CategoriesList  
        {  
            protected override void CreateChildControls()  
            {  
                //base.CreateChildControls();  
                if (this.manager == null || this.manager.Provider.Name != base.ProviderName)  
                    this.manager = new ContentManager(base.ProviderName);  
     
                this.categoriesListContainer = new CategoriesListContainer(this);  
     
                if (this.template == null)  
                {  
                    string path = String.IsNullOrEmpty(this.TemplateUrl) ?  
                            this.manager.Provider.CategoriesListPublicTemplate : this.TemplateUrl;  
                    if (this.DesignMode)  
                    {  
                        Page page = new Page();  
                        this.template = page.LoadTemplate(path);  
                    }  
                    else if (this.Page != null)  
                    {  
                        if (File.Exists(this.Page.MapPath(path)))  
                            this.template = this.Page.LoadTemplate(path);  
                        else this.template = new DefaultTemplate();  
                    }  
                }  
                this.template.InstantiateIn(this.categoriesListContainer);  
     
                this.categoriesListContainer.CategoriesRepeater.DataSource = this.manager.GetCategories(0, 0, "CategoryName ASC");  
                this.categoriesListContainer.CategoriesRepeater.ItemDataBound += new RepeaterItemEventHandler(CategoriesRepeater_ItemDataBound);  
                this.categoriesListContainer.CategoriesRepeater.DataBind();  
     
                if (this.categoriesListContainer.ControlTitle != null)  
                    this.categoriesListContainer.ControlTitle.Text = this.ControlTitle;  
     
                this.Controls.Add(this.categoriesListContainer);  
            }
            #region Event handlers  
     
            private void CategoriesRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)  
            {  
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)  
                {  
                    ICategory category = (ICategory)e.Item.DataItem;  
                    HyperLink categoryLink = (HyperLink)e.Item.FindControl("categoryLink");  
                    categoryLink.Text = category.CategoryName;  
     
                    if (this.CategoryKeyType == CategoryKeyTypes.ID)  
                        categoryLink.NavigateUrl =  
                            ResolveUrl(String.Concat(this.CategoryContentUrl, "?"this.CategoryKey, "=", category.ID.ToString()));  
                    else 
                        categoryLink.NavigateUrl =  
                            ResolveUrl(String.Concat(this.CategoryContentUrl, "?"this.CategoryKey, "=", category.CategoryName));  
     
                    IUrlService svc = UrlServices.GetLanguageService();  
                    if (svc != null)  
                    {  
                        categoryLink.NavigateUrl = svc.ResolveLanguageUrl(categoryLink.NavigateUrl);  
                    }  
     
                    ITextControl categoryContentCount = (ITextControl)e.Item.FindControl("categoryContentCount");  
                    categoryContentCount.Text = category.ContentCount.ToString();  
                }  
            }
            #endregion  
     
            #region Private fields  
     
            private ITemplate template;  
            private CategoriesListContainer categoriesListContainer;  
            private ContentManager manager;
            #endregion  
        }  
    }  
     

    Also, you should declare the newly created control in the <toolboxControls> section of the web.config file to display it in "Add Controls" toolbox:

    <add name="Custom CategoriesList" section="Categories" type="Telerik.Cms.Engine.WebControls.Categories.CustomCategoriesList, App_Code" /> 

    Do let us know if this helps.

    Greetings,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  7. Damian
    Damian avatar
    42 posts
    Registered:
    25 Mar 2008
    24 Oct 2008
    Link to this post

    Hello Pepi and everyone,

    I am now using Sitefinity v 3.5

    and am trying to sort the category list for a document library using the above code.

    The categories appear in the right order, but the number next to the category name in brackets is 0 for all categories.

    How can I fix this ? Does this code need changing because of v 3.5

    Thanks

    Damian

    Thanks

    Damian

  8. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    29 Oct 2008
    Link to this post
    Hi Damian,

    You need to use the following overload of GetCategories method:

    public IList GetCategories(  
       int from, 
       int to, 
       string sortExpression, 
       bool calculateContentCount 

    So the CreateChildControls should look as follows:

    protected override void CreateChildControls() 
            { 
                ... 
     
                this.categoriesListContainer.CategoriesRepeater.DataSource = this.manager.GetCategories(0, 0, "CategoryName ASC"true); 
                this.categoriesListContainer.CategoriesRepeater.ItemDataBound += new RepeaterItemEventHandler(CategoriesRepeater_ItemDataBound); 
                this.categoriesListContainer.CategoriesRepeater.DataBind(); 
                ... 

    Hope this helps.

    Regards,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Register for webinar
8 posts, 0 answered