Monday, January 4, 2016

Extend Custom Sort in Sitecore.

Implementation Steps

  1. Go to system ->settings-> subitems sorting create new item with template Sytem/Child Sorting
  2. Create below Demo.Sitecore.Practical.CustomSortComparer.dll and deploy in CMS instance bin folder.
  3. Add Type and class assembly details under Data section for Newly created item Created Date( instance of sitecore Setting Subitems sorting.
  4. You can sort using subitem sorting for any sitecore tree using Custom Created Date.


Class Library

using System;
using System;
using Sitecore.Data.Comparers;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;

namespace Demo.Sitecore.Practical.CustomSortComparer
{
    public class SitecoreCustomDateFieldComparer: Comparer
    {
        protected override int DoCompare(Item item1, Item item2)
        {
            var date1 = GetDateTime(item1);
            var date2 = GetDateTime(item2);
            return date1.CompareTo(date2);
        }

       private static DateTime GetDateTime(Item item)
        {
            var dateField = (DateField)item.Fields["Date"];
            return dateField != null ? dateField.DateTime : DateTime.MinValue;
        }
    }
}


No comments :