Home > C#, Sitecore 6 > Custom Subitems sorting

Custom Subitems sorting

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

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 !

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

This is done in the Data-section.

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
{<br>
/// <summary>
/// Initializes a new instance of the <see cref="CreatedComparer"/> class.
/// </summary><br>
/// <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.

Advertisements
Categories: C#, Sitecore 6 Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: