Creating a Computed Date Field
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
-
Create a new C# project named
Coveo.Custom
that references theSitecore.ContentSearch
and theSitecore.Kernel
assemblies (typically located under<SITECORE_INSTANCE_ROOT>\website\bin
). -
Create a new class named
ArchiveDateField
. It should be located in theCoveo.Custom
namespace. -
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 thedatetime
string and theComputeFieldValue
method must return the date formatted asyyyy/MM/dd@HH:mm:ssZ
. -
Compile your project.
-
Copy the resulting assembly (
Coveo.Custom.dll
) to thebin
folder of your Sitecore instance.
Step 2: Configuring the Computed Field
-
Using a text editor, open the
Coveo.SearchProvider.Custom.config
file (typically located underApp_Config\Include\Coveo
). -
Add the computed field in the
fields
section ofCoveo.SearchProvider.Custom.config
as described in Creating a Computed Field for a Referenced Item. Set the value of thefieldName
attribute toarchivedate
. -
Add the field to your
fieldmap
. -
Make your field sortable (
isSortable="true"
), facetable (isFacet="true"
), and most importantly, specify its type and return type toSystem.DateTime
(type="System.DateTime"
andreturnType="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
-
Access the Indexing Manager of Sitecore (Sitecore Desktop > Start Menu > Control Panel > Indexing > Indexing Manager).
-
Rebuild your indexes.
-
Validate that the field now exists in the Coveo index:
-
Open the Index Browser inside the CES Administration Tool.
-
Find an item located inside your Sitecore instance.
-
Examine the item fields by expanding the Details > Fields tab.
-
Validate that the
archivedate
field exists and that it’s of typeDate/time
. Its value should be one year after the item’s creation date.
-