Custom Subitems sorting

13. november 2009 by Thomas Stern

Subitems sorting in sitecore only works for some of predefined fields. But it is an easy task to sort items for none standardfields. Subitems sorting is found in Sorting dialog here

sorting

 

First you need to create your own subitem sorting. This can be found in the master database under system/setting/subitems sorting

Easiest thing to do is just to duplicate one of the existing items and edit the duplicated item.

The name of the items will be shown in the sorting list so give i a meaning full name, i choose own created as an example. Bad name !

subsorting

 

You need to specifiy in which assambly your compare class is presented in.

This is done in the Data-section.

defiinedll

So for this example we created a our own date field but not all items derives from a template that gives acces to this field, so as a fallback we look at the sitecore __created field "standard field under statistics.

The keypart is the DoCompare function in the following code

namespace PT.SubItemsSorter
{
 public class CreatedComparer : Comparer
 {
/// <summary> /// Initializes a new instance of the <see cref="CreatedComparer"/> class. /// </summary>
/// <remark>Created 13-11-2009 11:55 by ts</remark> public CreatedComparer( } // Methods protected override int DoCompare(Item item1, Item item2) { return GetCreatedDate(item2).CompareTo(GetCreatedDate(item1)); } /// <summary> /// Gets the created date. /// </summary> /// <param name="item">The item.</param> /// <returns></returns> /// <remark>Created 13-11-2009 11:55 by ts</remark> private DateTime GetCreatedDate(Item item) { if (String.IsNullOrEmpty(item[DatasourceCreatedField]) return GetDate(item,SitecoreCreatedDateField); return GetDate(item, DatasourceCreatedField); } /// <summary> /// Gets the date. /// </summary> /// <param name="item">The item.</param> /// <param name="field">The field.</param> /// <returns></returns> /// <remark>Created 13-11-2009 11:55 by ts</remark> private DateTime GetDate(Item item,string field) { DateField dateField = ((DateField)item.Fields[field]); return dateField.DateTime; } private const string DatasourceCreatedField = "DataSource_CreatedDate"; private const string SitecoreCreatedDateField = "__Created"; } }

Now you should be able to se it in the subitems sorting list. The code gives you the newest first.

alldonesorting

 

0 comment(s) for “Custom Subitems sorting”

    Leave comment: