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

Forums / Developing with Sitefinity / sorting the category selection list

sorting the category selection list

16 posts, 0 answered
  1. Dave
    Dave avatar
    8 posts
    Registered:
    23 Apr 2008
    07 Jan 2009
    Link to this post
    When creating a new event, there is a dropdown list of categories that have been created using the Categories list within the Event module.   This list of categories is sorted by GUID ( I think ), but would be MUCH more useful if sorted by category name.
    What do I do to change the sort order and why would the product even think to have a default sort by GUID?

    Alan
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    07 Jan 2009
    Link to this post
    Hello Alan,

    Below is an example about how to get events from a certain category.


     // add category 
        string category = "mycategory"
     
        //create a new instance of EventsManager 
         EventsManager eventsManager = new EventsManager(); 
         
       //create filter here 
       List<IMetaSearchInfo> filter = new List<IMetaSearchInfo>(); 
       Filter.Add(new MetaSearchInfo(MetaValueTypes.ShortText, "Category", category, SearchCondition.Equal, JoinType.Add)); 
     
        // get events using GetContent(); 
        IList listOfAllEvents = eventsManager.Content.GetContent(0, 0, string.Empty, nullstring.Empty, null, filter.ToArray()); 
        foreach(IContent eventItem in listOfAllEvents) 
        Response.Write(eventItem.GetMetaData("Title").ToString() + "<br />"); 

    I hope this helps.

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Dave
    Dave avatar
    8 posts
    Registered:
    23 Apr 2008
    07 Jan 2009
    Link to this post

    Hi Ivan,

    I think this example shows how to return a list of events or event details for a specific category.  This is useful to know, thanks, but not exactly what I was asking.

    I am referring to the dropdown list of categories you can choose from when adding (creating) a new event.  I am creating a new event and before I save it, I am selecting a category from the dropdown control (in the event editor).  The problem is that this list of categories is not sorted by category name.  It appears to be sorted by GUID.  My list of categories is going to be fairly long and my users will not enjoy hunting for the desired category in a long, randomly sorted listing.

    I hope there is a solution to this.

    Thanks very much for your help.

    Alan

     

  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    08 Jan 2009
    Link to this post
    Hello Alan,

    Ok, I got it now. I thought about some custom implementation of getting the categories. However, categories in the drop down are not sorted by name. Unfortunately there is no workaround for this and we have to change the source. We will try to have this fixed in 3.6 in the end of January.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  5. Brook
    Brook avatar
    39 posts
    Registered:
    21 Mar 2007
    11 Jan 2009
    Link to this post
    If the category control could have the option of both sortation and filtration would be great.

    ~Brook 
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    12 Jan 2009
    Link to this post
    Hi Brook,

    The category names will be sorted alphabetically. As for the filtration, we will try to allow the users to make changes, but I am not sure if this will be possible in 3.6. We started implementing this a few days ago.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  7. KMac
    KMac avatar
    133 posts
    Registered:
    15 Dec 2008
    03 Apr 2009
    Link to this post
    Hello,

    Just wondering if this was fixed in 3.6 and if so, was it also fixed for the news module. I'm adding categories there and they seem to populate in random orders, sometimes at the end, sometimes at the beginning and sometimes somewhere in the middle.

    Is there some way I can just make these categories sort by name?

    Thanks in advance,

    Kalvin
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    06 Apr 2009
    Link to this post
    Hi Kalvin MacLeod,

    It is not fixed in 3.6, but I will give you a workaround. Note that this example will not work under versions 3.5
    Here are the steps.

    1. Create a new class in App_Code folder.
    2. Override ContentCategoriesField control

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using Telerik.Cms.Engine.WebControls.Admin; 
    using Telerik.Web.UI; 
    using System.Collections; 
    using Telerik.Cms.Engine; 
     
    /// <summary> 
    /// Summary description for MyContentCategoriesField 
    /// </summary> 
    ///  
     
    namespace Telerik.Samples 
        public class MyContentCategoriesField : ContentCategoriesField 
        { 
            public MyContentCategoriesField() 
            { 
            } 
     
            // set the template for CategoriesField.ascx 
            public override string LayoutTemplatePath 
            { 
                get 
                { 
                    if (string.IsNullOrEmpty(this.layoutTemplatePath)) 
                        this.LayoutTemplatePath = "~/Sitefinity_3_6_1870_standard_ExternalTemplates/Sitefinity/Admin/ControlTemplates/Generic_Content/CategoriesField.ascx"
                      return this.layoutTemplatePath; 
                } 
                set 
                { 
                    this.layoutTemplatePath = value; 
                } 
            } 
     
            // override initialize controls 
            protected override void InitializeControls(System.Web.UI.Control controlContainer) 
            { 
                base.InitializeControls(controlContainer); 
                this.CategoriesList.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(CategoriesList_SelectedIndexChanged); 
                this.CategoriesList.Items.Clear(); 
                 
                // get categories by CategoryName ASC 
                IList categories = this.Manager.GetCategories(0, 0, "CategoryName ASC"); 
                RadComboBoxItem liUncategorized = new RadComboBoxItem(); 
                liUncategorized.Text = "-- uncategorized --"
                liUncategorized.Value = string.Empty; 
                if (String.IsNullOrEmpty(selectedCategory)) 
                    liUncategorized.Selected = true
                this.CategoriesList.Items.Add(liUncategorized); 
                AddCategory(Guid.Empty, categories); 
     
     
            } 
     
            /// <summary> 
            /// Adds the category. 
            /// </summary> 
            /// <param name="parentId">The parent id.</param> 
            /// <param name="categories">The categories.</param> 
            private void AddCategory(Guid parentId, IList categories) 
            { 
                foreach (ICategory category in categories) 
                { 
                    if (this.CategoriesList.FindItemByValue(category.ID.ToString()) == null && category.ParentCategoryID == parentId) 
                    { 
                        RadComboBoxItem categoryItem = new RadComboBoxItem(category.CategoryName, category.CategoryName); 
                        int level = CalculateLevel(category, categories); 
     
                        if (level > 0) 
                            categoryItem.Style.Add("padding-left"string.Concat((10 * level), "px")); 
                        if (category.CategoryName == selectedCategory) 
                            categoryItem.Selected = true
     
                        this.CategoriesList.Items.Add(categoryItem); 
                        AddCategory(category.ID, categories); 
                    } 
                } 
            } 
     
            /// <summary> 
            /// Calculates the level. 
            /// </summary> 
            /// <param name="category">The category.</param> 
            /// <param name="categories">The categories.</param> 
            /// <returns></returns> 
            private static int CalculateLevel(ICategory category, IList categories) 
            { 
                int level = 0; 
                while (category.ParentCategoryID != Guid.Empty) 
                { 
                    level++; 
                    foreach (ICategory category1 in categories) 
                    { 
                        if (category.ParentCategoryID == category1.ID) 
                        { 
                            category = category1; 
                            break
                        } 
                    } 
                } 
                return level; 
            } 
     
            void CategoriesList_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e) 
            { 
                //Controls.Clear(); 
                RadComboBox combo = new RadComboBox(); 
                if (combo.SelectedItem.Value != String.Empty) 
                    selectedCategory = combo.SelectedItem.Value; 
                Controls.Add(combo); 
            } 
            private string selectedCategory; 
            private string layoutTemplatePath; 
        } 



    3. Remove the current control from the template you want to have sorted categories.
    <%--<sf:ContentCategoriesField ID="Category" runat="server" />--%> 

    4. Register an new TagPrefix and declare our control in the template. The control must be with ID="Category"

    <%@ Register TagPrefix="cus" Namespace="Telerik.Samples" Assembly="App_Code" %> 

    <cus:MyContentCategoriesField ID="Category" runat="server"></cus:MyContentCategoriesField> 

    I hope this helps.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  9. Jesse
    Jesse avatar
    4 posts
    Registered:
    07 Mar 2008
    08 Apr 2009
    Link to this post

    Okay.. I think I understand the example.. thank you for the code..

    One question though,  where exactly do I swap out the control reference?

    Im digging through the /SiteFinity/Admin/ControlTemplates folders.. and I see Generic Content and I see News Module... both of them only have a COntentVersionView.aspx file in them..

    I am using SiteFinity 3.6

    Also..  In those pages I can see the control with the ID of "Category"  but it is an ASP:Literal control, not the sf:ContentCategoriesField   as you have specified.

     

  10. Jesse
    Jesse avatar
    4 posts
    Registered:
    07 Mar 2008
    08 Apr 2009
    Link to this post
    Okay I solved my problem with this..

    The key (which wasnt really mentioned well in this thread) was the mapping of a custom view.   Since I am using 3.6, the control templates for swapping out the NewsItemNew and NEwsItemEdit views werent in the source tree.  It was driving me nuts!

    I stumbled on this KB Article about the mapping of views..  
    http://www.sitefinity.com/support/knowledge-base/kb-article/b1154K-bcge-b1154T-cgb.aspx

    Its at least an explanation of why I wasnt seeing the control template views that are referenced all over the various posts and blogs and code samples. and once I followed the instructions in that guide, and combine it with the advice in this thread I was able to show Categories in Ascending order in the News admin screens.

    Also.. another useful tip with this whole type of thing is that you have to download the ExternalTemplates from your downloads section.. Its a zipped file that has all of the templates for the various views in the SiteFinity admin section. 


  11. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    08 Apr 2009
    Link to this post
    Hello Jesse,

    When you want to make any changes to an template you should map it and use it as an external one. In the zip there is a ControlsConfig file with host types that you should use. In the provided example we are replacing the base control with ID="Category" with our custom one but we are using the same ID,because the control with ID="Category" is required.

    I hope this will help to all community members using our product.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  12. Karl
    Karl avatar
    45 posts
    Registered:
    18 Jun 2009
    05 May 2011
    Link to this post
    Hi,

    I've implemented the MyContentCategoriesField : ContentCategoriesField which seems to work ok. How ever when I go to edit the document again the category it is saved into is never selected. It just defaults to uncategorized, the category is defiantly saved because when I click the view tab I can see the category name.

    Why is the category not coming back selected? Am I missing something?

    Thanks
  13. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    05 May 2011
    Link to this post
    Hi,

    Please check categoryItem.Selected = true; and the value that is set in this row of the sample.

    Best wishes,
    Ivan Dimitrov
    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
  14. Karl
    Karl avatar
    45 posts
    Registered:
    18 Jun 2009
    05 May 2011
    Link to this post
    Thanks for the quick reply, what do you mean by checking?

    At what point does the selectedCategory get populated with a value? I've stepped through the file and it never gets populated, it's always null.
  15. Karl
    Karl avatar
    45 posts
    Registered:
    18 Jun 2009
    05 May 2011
    Link to this post
    I found the below in another post and looks to have fixed it:
    if (content == null && ContentId != Guid.Empty)
                    content = this.Manager.GetContent(ContentId);
                if (content != null)
                {
                    object objSelectedCategory = content.GetMetaData("Category");
                    selectedCategory = objSelectedCategory != null ? objSelectedCategory.ToString() : String.Empty;
  16. Julio
    Julio avatar
    1 posts
    Registered:
    06 May 2011
    06 May 2011
    Link to this post
    Thanks everyone. The codes posted above helped me out. 
Register for webinar
16 posts, 0 answered