Sorting Limitation in the Admin/Config/Edit Categories

  • Updated

There is a sorting limitation in the Admin/Config/Edit Categories.  This article will provide sample workaround code to sort these categories.

EPiServer categories seems to have no built-in sorting when retrieving them as a list when using CategoryList. Each one of the specific categories do have a SortOrder property.

The sample below uses that SortOrder property and the generic SortedList class to sort the categories, like this:

SortedList<int, ListItem> sortedCategories = new SortedList<int, ListItem>();
CategoryList cats = CurrentPage.Category;
foreach (int c in cats)
{
Category cat = Category.Find(c);
string catName = Category.Find(cats.GetCategoryName(c)).Name; ListItem li = new ListItem(catName, catName);
sortedCategories.Add(cat.SortOrder, li);
} ddCategories.Items.AddRange(sortedCategories.Values.ToArray());

The code can be implemented in a custom gadget or on an admin template.