Create a computed field for a referenced item

A Sitecore field can reference another Sitecore item, which contains several fields of its own. Sometimes, you may need to retrieve a field from the referenced item to use it in a computed field.

Logic behind Coveo for Sitecore out-of-the-box computed fields | Coveo for Sitecore 5

This can be done using the referencedfieldcomputedfield computed field.

This article provides an example showing how to use the out-of-the-box ReferencedFieldComputedField computed field.

Scenario

Your content tree contains items of type Flight and items of type Airport.

A Flight contains the following fields:

Field name Field type Example value

Flight Number

Single-Line Text

3836

Departure Airport

Droptree (reference)

Airports/2/3/8/2/0/Amsterdam Airport Schiphol (raw value is actually {23820EE7-87A3-4265-B785-2A1D25C98F72}).

Departure Time

Datetime

5/23/2014 10:01 PM

Arrival Airport

Droptree (reference)

Airports/9/C/2/F/4/Los Angeles International Airport (raw value is actually {9C2F4988-0951-443C-9526-5DBB5D8D9C02}).

Arrival Time

Datetime

5/24/2014 12:16 AM

Price

Single-Line Text

586

An Airport contains the following fields:

Field name Field type Example value

Airport Name

Single-Line Text

Amsterdam Airport Schiphol

Airport Code

Single-Line Text

AMS

City

Single-Line Text

Amsterdam

State

Single-Line Text

Country

Single-Line Text

Netherlands

Time Zone

Droplink

(+1:00) Central Europe Standard Time

Latitude

Number

52

Longitude

Number

5

Aliases

Single-Line Text

Your goal is to populate a departureairportcity field in Flight items by retrieving the content of the City field from a referenced Airport item. In other words, you want to do this:

Logic behind ReferencedFieldComputedField in flight and airport example | Coveo for Sitecore 5

Step 1: Configure the computed field

You must first configure your computed field in the Coveo Search Provider configuration file.

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

  2. Locate the fields element with a hint attribute equal to raw:AddComputedIndexField.

  3. Insert the configuration required for your computed field as shown below (that is, just the inner field element).

     <fields hint="raw:AddComputedIndexField">
       <field fieldName="departureairportcity" sourceField="Departure Airport" referencedFieldName="City">Coveo.SearchProvider.ComputedFields.ReferencedFieldComputedField, Coveo.SearchProviderBase</field>
     </fields>

    Here are the available options for your computed field configuration:

    Name Description Example value
    fieldName The name of the new field to create departureairportcity
    sourceField The field containing the referenced item's ID Departure Airport
    referencedFieldName The name of the field on the referenced item City
    returnType Return type of the field The possible values are: Integer, Number, date, datetime, and string.
    Note

    This configuration lets you create and index the computed field. If you want to apply special settings to that field (for example, to use it as a facet or to sort results), you also need to add a configuration in the fieldNames section of the configuration file.

Step 2: Rebuild your indexes

Important

To have the computed field created, the related sourceField and referencedFieldName fields must be in the <include> fields list.

To be able to use your newly created computed field, you now need to perform an indexing action. Rebuild your search indexes, or at least re-index the Sitecore items which need the new field.

Step 3: Validate the computed field has been added

Validate that your new field has been added on your items using the Content Browser (platform-ca | platform-eu | platform-au).

In this example, Flight items should now have a field named departureairportcity that contains the city of the departure airport.

Flight departure airport city computed field value in Content Browser | Coveo for Sitecore 5

Step 4: Use the computed field

You can now use your computed field in your layouts and result templates.