THIS IS ARCHIVED DOCUMENTATION

Coveo for Sitecore Legacy Search UI Framework - Coveo Fields Resources Component

Coveo for Sitecore (October 2016) Coveo for Sitecore (November 2018)

The Coveo Fields Resources component provides a simple JavaScript object that lets the user translate fields to the Coveo format.

It’s intended to replace the server side call @Model.ToCoveoFieldName.

This page presents the recommended way of handling Coveo fields in a Coveo result template.

Using the Library

To use the library, you need to add the Coveo Fields Resources component to your layout. For an MVC page, you should instead include the Coveo Fields Resources View component.

For more information on how to add components to your layout, see Inserting Coveo Search Components to an Existing Item.

The @ character

One of the differences between the Coveo Fields Resources component and the Model.ToCoveoFieldName method is the use of the @ character.

When calling Model.ToCoveoFieldName using a field name, by default, the @ character is prepended to the field name.

Model.ToCoveoFieldName("field")

If the second Model.ToCoveoFieldName parameter is set to false, the @ character isn’t prepended to the field name.

Model.ToCoveoFieldName("field", false)

The new library automatically handles the @ character, removing the second parameter. It makes the code more readable, as you can translate both @_id and _id in the same manner, and it’s shown visually instead of using a Boolean value.

JavaScript Object

When the component is included, it’s possible to access a JavaScript object using CoveoForSitecore.fields.toCoveo().

Calling .toCoveo("<your field>") translates your field variable to the Coveo format.

Calling CoveoForSitecore.fields.toCoveo("_id") returns fz95xidXXXXX, where XXXXX is the Sitecore instance hash used by Coveo for Sitecore fields.

If the field is to be used as the Coveo format, such as when adding a new expression to the advancedExpression part of the query, it’s possible to translate the field starting with the @ character.

Calling CoveoForSitecore.fields.toCoveo("@_id") returns @fz95xidXXXXX, where XXXXX is the Sitecore instance hash used by Coveo for Sitecore fields.

Result Template Helpers

The component also registers result template helpers to use in the Coveo Search UI Framework.

coveoFieldValue

Returns the value for the field name.

Calling coveoFieldValue("_id") returns the value for the fz95xidXXXXX field.

coveoFieldName

Returns the translated field name, just like CoveoForSitecore.fields.toCoveo.

Calling coveoFieldName("_id") returns fz95xidXXXXX.

Example

In the following result template sample, you can replace all of the ToCoveoFieldName calls.

Underscore Template

<script class="result-template" type="text/underscore">
    ... // Omitted for this example
    <table class="CoveoFieldTable">
        <tbody>
            <tr data-field="<%= ToCoveoFieldName("_templatename") %>" data-caption="Template Name" />
            {{ if (raw.<%= ToCoveoFieldName("created", false) %> === raw.<%= ToCoveoFieldName("updated", false) %>) { }}
                <tr data-field="<%= ToCoveoFieldName("parsedcreatedby") %>" data-caption="Created By" />
            {{ } }}
        </tbody>
   </table>
</script>

On the fifth line, you can see the following:

<tr data-field="<%= ToCoveoFieldName("_templatename") %>" data-caption="Template Name" />

Since it needs a field name, the server call can be replaced with the following:

<tr data-field="{{= coveoFieldName("@_templatename") }}" data-caption="Template Name" />

You need to append the @ character since the data-field needs a Coveo field name as its value.

When using MVC, remember to double the @ character to use it in a field name to avoid Razor errors.

On the sixth line, you can see the following:

if (raw.<%= ToCoveoFieldName("created", false) %> === raw.<%= ToCoveoFieldName("updated", false) %>)

Since it needs a field value, extracted from the raw object, it can be replaced with the following:

if (coveoFieldValue("created") === coveoFieldValue("updated"))

Both previous ToCoveoFieldName calls were setting the second parameter to false, meaning the new calls don’t need the @ character before the field names.

The full translated code would look like this:

<script class="result-template" type="text/underscore">
    ... // Omitted for this example
    <table class="CoveoFieldTable">
        <tbody>
            <tr data-field="{{= coveoFieldName("@_templatename") }}" data-caption="Template Name" />
            {{ if (coveoFieldValue("created") === coveoFieldValue("updated")) { }}
                <tr data-field="{{= coveoFieldName("@parsedcreatedby") }}" data-caption="Created By" />
            {{ } }}
        </tbody>
   </table>
</script>

The latter is easier to read and understand, which makes it the recommended way of handling Coveo fields in a result template.