THIS IS ARCHIVED DOCUMENTATION

Creating a Computed Date Field

Computed fields can also be used to create entirely new fields from the data found in an item existing fields. For example, if you wanted to create some kind of archiving system in Sitecore, you would need to create a field containing the archive date of an item, which would be based on an item creation date.

Step 1: Creating the Required C# Project

  1. Create a new C# project named Coveo.Custom that references the Sitecore.ContentSearch and the Sitecore.Kernel assemblies (typically located under <SITECORE_INSTANCE_ROOT>\website\bin).

  2. Create a new class named ArchiveDateField. It should be located in the Coveo.Custom namespace.

  3. Paste the following code in the class.

    public class ArchiveDateField : IComputedIndexField
    {
        // The 'Z' at the end of the format string tells CES
        // 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;
        }
    }
    

    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.

  4. Compile your project.

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

Step 2: Configuring the Computed Field

  1. Using a text editor, open the Coveo.SearchProvider.Custom.config file (typically located under App_Config\Include\Coveo).

  2. Add the computed field in the fields section of Coveo.SearchProvider.Custom.config as described in Creating a Computed Field for a Referenced Item. Set the value of the fieldName attribute to archivedate.

  3. Add the field to your fieldmap .

  4. Make your field sortable (isSortable="true"), facetable (isFacet="true"), and most importantly, specify its type and return type to System.DateTime (type="System.DateTime" and returnType="System.DateTime").

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

Step 3: Rebuilding Your Indexes

  1. Access the Indexing Manager of Sitecore (Sitecore Desktop > Start Menu > Control Panel > Indexing > Indexing Manager).

  2. Rebuild your indexes.

  3. Validate that the field now exists in the Coveo index:

    1. Open the Index Browser inside the CES Administration Tool.

    2. Find an item located inside your Sitecore instance.

    3. Examine the item fields by expanding the Details > Fields tab.

    4. Validate that the archivedate field exists and that it’s of type Date/time. Its value should be one year after the item’s creation date.