Creating a Computed Field for a Referenced Item
Creating a Computed Field for a Referenced Item
A Sitecore field can refer to a Sitecore item, which itself contains several fields of its own. Sometimes, you may need to display the fields that are only part of the referenced item in a search interface. This way, you end up with complementary information for each one of your search results. This can be easily done by using what’s called a computed field.
Requirements
-
You have a running instance of the Jetstream Demo (version 2.0) and you generated a few flights using the Generate Flights feature.
Although this tutorial is based on Jetstream, you can easily adapt the provided code to your specific situation. The underlying principle stays exactly the same.
Putting It Into Context
Jetstream contains items of type Flight (located under sitecore/Content/Global/Flights
). A Flight is composed of 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 |
If you choose to display raw field values (under View > Raw Values), you will discover that the Departure and Arrival Airport fields actually contain IDs or GUIDs. This is because they point to items of type Airport (located under sitecore/Content/Global/Airports
).
An Airport is composed of 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 |
For each search result that represents a Flight item, you may want to display additional fields, such as the related Departure Airport City or Departure Airport Code field. To do that, you need to create a computed field.
Step 1: Configuring the Computed Field
You must first configure your computed field in the Coveo Search Provider configuration file.
-
Using a text editor, open the
Coveo.SearchProvider.Custom.config
file (typically located underApp_Config\Include\Coveo
). -
Locate the
fields
element with ahint
attribute equal toraw:AddComputedIndexField
. -
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>
Available Options
Here are the available options for your computed field:
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 , and datetime . |
Its value should be formatted as <complete name of the class including namespace>, <assembly>
.
Since you can customize a computed field like any other field and you can make it sortable, it supports many features, such as free-text search, etc. For more information, see FieldMap. Note that in such a field map, fieldName
must have the same value as the one defined in the previous section.
Step 2: Rebuilding Your Indexes
To be able to use your newly created computed field, you now need to rebuild your Sitecore indexes.
-
Access the Indexing Manager of Sitecore (Sitecore Desktop > Start Menu > Control Panel > Indexing > Indexing Manager).
-
Rebuild your indexes.
In this example, Flight items should now have a field named departureairportcity
that contains the city of the departure airport associated with the flight.
Step 3: Using the Computed Field
Once you have rebuilt your indexes, you should be able to use your computed field in your layouts and sublayouts, just like any other Sitecore field. To refer to it, remember that you need to use the custom extension function named ToCoveoFieldName
.