Often you get stuck with things you don't want to do again and again, one of them is to bang your head against the wall trying to figure out where you wrote a icomparer that you can easily use to get a sorted ChildList. Well, after banging my head enough i wrote a small workaround using a DataContext instead.
The method is pretty straight-forward, so just use it as it is or modify it to any need.
private Sitecore.Collections.ItemCollection GetChildrenSorted(Item parent, String sortby, bool sortascending, int offset, int range, string filter)
{
Sitecore.Web.UI.HtmlControls.DataContext dc = new DataContext();
dc.DataViewName = "Master";
return dc.DataView.GetChildren(parent, sortby, sortascending, offset, range, filter);
}
So, whenever you need to get a list of items sorted a specific way just use the method in the appropriate way, maybe like the example below?
private void FillSomeListOrSomething()
{
// Parent Item that you want to get the children from..
Item parent = Sitecore.Configuration.Factory.GetDatabase("master").Items["/sitecore/content/home"];
if (parent.HasChildren)
{
foreach (Item itm in GetChildrenSorted(parent, "__created", false, 0, 0, ""))
{
// whatever you want to do with the sorted list of items you got back..
}
}
}
Maybe this is a really bad way to approach this problem, but it works for me so i'll use it for now (or until this functionality is implemented in Sitecore).
No comments:
Post a Comment