Pass Custom Parameters to Computed Fields

Sometimes, you may want to pass custom parameters to a computed field through the configuration. For example, following the Create a Computed Date Field tutorial, you may want to control when the archivedate field is created on items. This tutorial shows how to configure the number of days before an item is archived.

Prerequisite

You have already completed the Create a Computed Date Field tutorial.

Step 1: Update the Coveo.Custom Project

  1. Using Visual Studio, open the ArchiveDateField class.

  2. Add a constructor to accept additional XML configuration.

     private readonly XmlNode m_Configuration;
     public ArchiveDateField(XmlNode p_Configuration)
     {
         m_Configuration = p_Configuration;
     }
  3. Add a method to parse the configuration XML attributes and return their value.

     protected string GetConfigurationValue(string p_ConfigurationKey)
     {
         string configurationValue = null;
         if (m_Configuration != null && m_Configuration.Attributes != null) {
             XmlAttribute configurationAttribute = m_Configuration.Attributes[p_ConfigurationKey];
             if (configurationAttribute != null) {
                 configurationValue = configurationAttribute.Value;
             }
         }
         return configurationValue;
     }
  4. Add a property to retrieve the configuration value formatted as an integer.

     protected int DaysBeforeArchiving
     {
         get {
             int daysCount;
             if (!Int32.TryParse(GetConfigurationValue("daysBeforeArchiving"), out daysCount)) {
                 daysCount = 365; // One year by default
             }
             return daysCount;
         }
     }
  5. Change the ComputeFieldValue method to use the DaysBeforeArchiving property.

     /// <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);
                 return itemCreationDate.AddDays(DaysBeforeArchiving).ToString(COVEO_INDEX_DATE_FORMAT);
             }
         }
         return null;
     }
  6. Compile your project.

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

Step 2: Update the Computed Field Configuration

Open your Coveo.SearchProvider.Custom.config file and add the daysBeforeArchiving parameter as an XML attribute.

+

 <field fieldName="archivedate" daysBeforeArchiving="30">Coveo.Custom.ArchiveDateField, Coveo.Custom</field>

Step 4: Validate the Computed Field Values

Validate the archivedate field values on your items using the Content Browser (platform-eu | platform-au). There should now be a difference of 30 days between the created and archivedate fields values.

35455016
What's next for me?