Create a Computed Date Field

Suppose you want to create some kind of archiving system in Sitecore by adding a computed field whose value would be one year after the item creation date.

35455011

This article provides the steps to achieve this.

Note

If necessary, see Implement and Configure a Computed Field in Your Index for more details on each step.

Step 1: Create the Required C# Project

  1. Create a new C# project named Coveo.Custom that references the Sitecore.ContentSearch and Sitecore.Kernel assemblies.

  2. Create a new class named ArchiveDateField in the Coveo.Custom namespace with the following code.

    using System;
    using Sitecore;
    using Sitecore.ContentSearch;
    using Sitecore.ContentSearch.ComputedFields;
    
    namespace Coveo.Custom
    {
        public class ArchiveDateField : IComputedIndexField
        {
            // The 'Z' at the end of the format string tells Coveo
            // that the date value represents Universal time.
            private const string COVEO_INDEX_DATE_FORMAT = "yyyy/MM/dd@HH:mm:ssZ";
            private const string SITECORE_CREATED_FIELD = "__Created";
            /// <inheritdoc />
            public string FieldName { get; set; }
            /// <inheritdoc />
            public string ReturnType
            {
                get
                {
                    return "datetime";
                }
                set
                {
                }
            }
            /// <inheritdoc />
            public object ComputeFieldValue(IIndexable p_Indexable)
            {
                SitecoreIndexableItem indexableItem = p_Indexable as SitecoreIndexableItem;
                if (indexableItem != null)
                {
                    IIndexableDataField dateField = indexableItem.GetFieldByName(SITECORE_CREATED_FIELD);
                    if (dateField != null)
                    {
                        DateTime itemCreationDate = DateUtil.ParseDateTime(dateField.Value.ToString(), DateTime.MaxValue);
                        // Ensure that the date uses universal time
                        if (itemCreationDate.Kind != DateTimeKind.Utc)
                        {
                            itemCreationDate = itemCreationDate.ToUniversalTime();
                        }
                        return itemCreationDate.AddYears(1).ToString(COVEO_INDEX_DATE_FORMAT);
                    }
                }
                return null;
            }
        }
    }
    Important

    To create a computed date field, the ReturnType property must return the datetime string and the ComputeFieldValue method must return the date formatted as yyyy/MM/dd@HH:mm:ssZ.

  3. Compile your project.

  4. Copy the resulting assembly (Coveo.Custom.dll) to the bin folder of your Sitecore instance.

Step 2: Configure the Computed Field

  1. In Coveo.SearchProvider.Custom.config, add the following element as a child of the <fields hint="raw:AddComputedIndexField"> element.

    <field fieldName="archivedate">Coveo.Custom.ArchiveDateField, Coveo.Custom</field>
  2. In Coveo.SearchProvider.Custom.config, add the following element as a child of the <fieldNames hint="raw:AddFieldByFieldName> element.

    <fieldType fieldName="archivedate" settingType="Coveo.Framework.Configuration.FieldConfiguration, Coveo.Framework" returnType="System.DateTime" />

Step 4: Validate the Computed Field Has Been Added

Validate that your new field has been added on your items using the Content Browser (platform-eu | platform-au). Its value should be one year after the item creation date.

What's next for me?