Generic Multilist Computed Field in Coveo for Sitecore

Posted 08/10/2014 by Akshay Sura

Computed fields are very useful, especially if you are implementing a multi-site / multi-lingual Sitecore solution. In some of the previous blog posts, we have covered foreign keys, auto suggestions and other important topics related to Coveo JS Framework.

In this post we will concentrate on multilist fields and how to create ONE generic unit of code that can handle either text or guids in the computed fields. Generally if you were to add a computed field, you need a piece of configuration as shown below and a class to perform the work needed to generate the output for the computed field. 

This gets very tedious as you are creating multiple fields that store a delimited list of text or guids in the raw. Here is a great solution to cut down on code but achieve the same functionality. First we need configuration where we can call the same class but pass in the values. Here is a sample piece of config:

YOURNAMESPACE.Business.Search.ComputedFields.MultilistComputedField, YOURNAMESPACE.Business YOURNAMESPACES.Business.Search.ComputedFields.MultilistComputedField, YOURNAMESPACE.Business

As you can see, we are calling the same code to generate the computed field value but we are passing in additional parameters via the config to specify which fields we want processed. Now for some code! As with any other computed field, we need to create a class which inherits IComputedField. The difference is that we need to pull in the additional configuration. Checkout the code below:

The ComputedFieldValue method exists as in any other computed field implementation, the difference is the constructor that takes in the XmlNode and when the properties are called we extract the attributes. I hope this helps you with your computed fields, let me know if you have any questions or need additional information.