Creating computed index Date field with custom formatting in Sitecore

Reading Time: < 1 minute

There are couple of options how to index Date (DateTime) fields or basically any type of fields in Sitecore.

You can:

  1. use built-in (computed) fields
  2. create your own computed fields:

Built-in computed fields can be found in Sitecore.ContentSearch assembly under Sitecore.ContentSearch.ComputedFields namespace. You can use for example CreatedDate and UpdatedDate classes from there to do the job.

This blog post is about the latter option and I will show you how to create your own computed fields.

You can use custom computed fields that I have made below if you need to have a special formatted date and do not want to recompile code each time the format changes but want to just change configuration. There are various other reasons why to use custom computed fields…

Definition of the custom computed field would be:

​​<field fieldName=”Date” referencedFieldName=”__Updated” dateFormatPattern=”yyyyMMdd”>ToTheCoreSk.SC.Foundation.Search.ComputedFields.DateField, ToTheCoreSk.SC.Foundation.Search</field>

This definition needs to be placed under this element <fields hint=”raw:AddComputedIndexField”> under your Index Configuration definition <CustomIndexConfiguration> element in config. See patch file in my gist embedded below to inspire.

Let’s quickly take a look what parameters can be configured in field definition:

  • fieldName – Name of field in index
  • referencedFieldName – Name of field in Sitecore
  • dateFormatPattern – Date pattern used for formatting -> See Microsoft documentation for further details – In this case (yyyyMMdd) the output would be 20190527

And this is the patch file to add this computed field to my custom index (adjust it based on your needs) and also definition of my custom computed field that you need to incorporate into your solution:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="">
<CustomIndexConfiguration type="ToTheCoreSk.SC.Foundation.Search.AlgoliaIndexConfiguration, ToTheCoreSk.SC.Foundation.Search">
<DocumentOptions type="Sitecore.ContentSearch.DocumentBuilderOptions, Sitecore.ContentSearch">
<fields hint="raw:AddComputedIndexField">
<field fieldName="Date" referencedFieldName="__Updated" dateFormatPattern="yyyyMMdd">ToTheCoreSk.SC.Foundation.Search.ComputedFields.DateField, ToTheCoreSk.SC.Foundation.Search</field>

using System.Globalization;
using System.Xml;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.ComputedFields;
using Sitecore.ContentSearch.Diagnostics;
using Sitecore.Xml;
namespace ToTheCoreSk.SC.Foundation.Search.ComputedFields
public class DateField : IComputedIndexField
public DateField(XmlNode configurationNode)
FieldName = XmlUtil.GetAttribute("fieldName", configurationNode);
ReferencedFieldName = XmlUtil.GetAttribute("referencedFieldName", configurationNode);
DateFormatPattern = XmlUtil.GetAttribute("dateFormatPattern", configurationNode);
public string FieldName { get; set; }
public string ReturnType { get; set; }
public string ReferencedFieldName { get; set; }
public string DateFormatPattern { get; set; }
public virtual object ComputeFieldValue(IIndexable indexable)
var item = (SitecoreIndexableItem)indexable;
if (item?.Item == null)
CrawlingLog.Log.Error("DateField: indexable is not provided");
return string.Empty;
if (string.IsNullOrEmpty(FieldName))
CrawlingLog.Log.Error("DateField: FieldName is not provided");
return string.Empty;
var field = (Sitecore.Data.Fields.DateField)item.Item.Fields[ReferencedFieldName];
if (field == null)
CrawlingLog.Log.Debug($"DateField: Cannot find field '{ReferencedFieldName}'");
return string.Empty;
if (string.IsNullOrEmpty(DateFormatPattern))
CrawlingLog.Log.Debug($"DateField: DateFormatPattern is not provided");
return string.Empty;
return field.DateTime.ToString(DateFormatPattern, CultureInfo.InvariantCulture);

view raw


hosted with ❤ by GitHub

Code is self explanatory I think 🙂

Happy coding!

One comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.