Vulnerability in EPiServer.Forms

Try our conversational search powered by Generative AI!

CategoryList sorted by name


Is there a simple way to retrieve categories from a CategoryList object sorted by name?

By default, if I bind to a CategoryList the individual Category items are returned in Id order.

Jun 09, 2008 12:36

protected DataTable GetSortedCategories(string parentCategoryName)
  DataTable returnValue = new DataTable(parentCategoryName);
  returnValue.Columns.Add(new DataColumn("Name", "".GetType()));
  returnValue.Columns.Add(new DataColumn("Id", 0.GetType()));

  Category cat = Category.Find(parentCategoryName);
  IList categories = cat.GetList();

  foreach (object obj in categories)
    Category category = obj as Category;
    if (category != null)
      DataRow dataRow = returnValue.NewRow();
      dataRow[0] = category.Name;
      dataRow[1] = category.ID;
  DataView dv = new DataView(returnValue);
  dv.Sort = "Name ASC";
  return dv.ToTable();

Keep in mind that I haven't done much testing of the code.

best regards
// Stefan

Aug 12, 2008 18:42
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.