Sitecore API: Retrieve an item’s child items and apply custom sorting

In this article we will explore different methods to retrieve Sitecore item’s child items, and apply sorting via available Sitecore options and by custom code.

Retrieve a Sitecore item’s child items

We would have following methods to retrieve an item’s child items in Sitecore API:

  1. ChildList object – Using the ChildList object we can retrieve child items of a particular item in Sitecore. Refer the below code snippet to see how it works.
    Sitecore.Data.Items.Item homeItem = Sitecore.Context.Database.GetItem("/sitecore/content/Home/ABCD");
    if(homeItem != null){
         var children = new Sitecore.Collections.ChildList(homeItem);
         System.Text.StringBuilder sb = new System.Text.StringBuilder();
         foreach(Sitecore.Data.Items.Item item in children){
              sb.AppendLine(item.Paths.Path);
         }
         return sb.ToString();
    }
    return "Item not found";

     

  2. GetChildren() method – We can use Sitecore.Data.Items.Item.GetChildren() method to retrieve child items. This will give only immediate child items of a particular Sitecore item. Refer below code snippet to see how it works.
    private String GetChildrenUsingGetChildren()
    {
    	Sitecore.Data.Items.Item homeItem = Sitecore.Context.Database.GetItem("/sitecore/content/Home/ABCD");
    	if (homeItem != null)
    	{
    		var children = homeItem.GetChildren();
    		System.Text.StringBuilder sb = new System.Text.StringBuilder();
    		foreach (Sitecore.Data.Items.Item item in children)
    		{
    			sb.AppendLine(item.Paths.Path);
    		}
    		return sb.ToString();
    	}
    	return "Item not found";
    }

Make sure you do not use Axes.GetDescendants() method, because it retrieves recursivelly all chidren of all the children of the item, and puts a performance penalty.

Sort child items

In addition to sorting items manually, you can select a rule for sorting the children of an item.

For each item, Sitecore stores a numeric sort order value in the __Sortorder (Sitecore.FieldIDs.Sortorder) field defined in the Appearance section of the standard template. This sort order values are used by Sitecore user interface, developer APIs, and XML representations to order lists of items.

If children of an item are not manually sorted, Sitecore automatically applies the sorting rule specified in the __Subitems sorting field. The default sorting rule sorts items by name, but you can select different sorting rules, and even implement your own child sorting rules for each item.

Using LINQ, we can sort child items while developing a component. Refer below code snippet to see how it works.

private string GetChildrenOrderedByDisplayName(){
            var item = Sitecore.Context.Database.GetItem("/sitecore/content/Cities");
            if(item != null){
                StringBuilder sb = new StringBuilder();
                foreach(Sitecore.Data.Items.Item child in item.Children.OrderBy(x => x.DisplayName)){
                    //process child
                }
                return sb.ToString();
            }
            return "Item not found";
        }

You can also sort items by the value of a particular field.

private string GetChildrenOrderdByField(){
     var item = Sitecore.Context.Database.GetItem("/sitecore/content/Cities");
     if(item != null){
          StringBuilder sb = new StringBuilder();
          foreach(Sitecore.Data.Items.Item child in item.Children.OrderBy(x => x["FieldName"])){
               //process child
          }
          return sb.ToString();
     }
     return "Item not found";
}