--- title: Implement and configure a computed field in your index slug: '2320' canonical_url: https://docs.coveo.com/en/2320/ collection: coveo-for-sitecore-v5 source_format: adoc --- # Implement and configure a computed field in your index Computed fields let you add entirely new fields in your [Coveo organization](https://docs.coveo.com/en/185/), in addition to the Sitecore template fields. You can implement a computed field using the following steps: ## Step 1: Implement the computed field . Create a new C# class library project called `Coveo.Custom`. You'll need to reference the `Sitecore.ContentSearch` assembly. . Create a new class that inherits from `Sitecore.ContentSearch.ComputedFields.IComputedIndexField`. [source,c#] ``` using System; using Sitecore; using Sitecore.ContentSearch; using Sitecore.ContentSearch.ComputedFields;   namespace Coveo.Custom { public class MyComputedField : IComputedIndexField { /// /// This property should be left empty. Instead, use the FieldName attribute in the configuration file to /// specify the name of the computed field. /// public string FieldName { get; set; }   /// /// This property can optionally be used to specify the data type of the Sitecore field being currently processed. So instead of /// being automatically handled as a string (which is the default behavior), the field will be converted into the corresponding /// Coveo data type. If you leave the property as is, the value returned by ComputeFieldValue will be converted into /// a string automatically. Note that you can't specify the return type of the field in the configuration file. /// public string ReturnType { get; set; }   /// /// This method is what actually determines the value of the computed field. /// public object ComputeFieldValue(IIndexable p_Indexable) { // Returning null means that the field won't be created. // By default, any returned value is automatically converted into a string. return null; } } } ``` . Customize the class to meet your specific needs. > **Important** > > * A computed field is processed for every Sitecore item that's re-indexed. To minimize the impact on the duration of the indexing process > > ** Filter out items that your computed field shouldn't be added to. > > ** Avoid doing HTTP requests. > > ** Avoid querying a search index. > > ** Ensure you optimize your code. > > * Since computed field logic runs in the context of Sitecore, you can take advantage of using the Sitecore API. > **Note** > > The `ReturnType` property should be set to the Sitecore data type of the value returned by the computed field. > Ultimately, this value is converted to the corresponding Coveo data type. > If you don't set any return type, the value is automatically converted to a string, and therefore be of type String in the Coveo index. > Here are the supported Sitecore data types along with their corresponding Coveo data types. > |=== > | Sitecore data type | Coveo index data type > > | Integer > | Integer > > | Number > | Floating point > > | date > | Date/time > > | datetime > > (Ensure the `ComputeFieldValue` method returns the datetime value in `yyyy/MM/dd@HH:mm:ssZ` format.) > | Date/time > > | string > | String > |=== > **Note** > > To access the built-in fields of the item being processed by the computed field, cast the `IIndexable` parameter of the `ComputeFieldValue` method to `IIndexableBuiltinFields`. > For example, do the following to access the template name: > > [source,c#] > ``` public object ComputeFieldValue(IIndexable p_Indexable) { IIndexableBuiltinFields fields = (IIndexableBuiltinFields)p_Indexable; string templateName = fields.TemplateName; ... } ``` . Compile the project. . Copy the `Coveo.Custom.dll` to the `bin` folder of your Sitecore instance (typically `\bin` or `\website\bin`). ## Step 2: Add the computed field in the configuration . Open the `Coveo.SearchProvider.Custom.config` file in a text editor. This file is typically located in `\App_Config\Include\Coveo` or `\website\App_Config\Include\Coveo`. . Locate the `` element. ```xml ``` . Add a new `` element as an immediate child of the `` element. ```xml MyNamespace.ComputedFields.MyComputedField, MyNamespace.ComputedFields ``` Replace the `fieldName` and XML element text values with the name you want for the Coveo index field, the proper full class name, and the assembly name. > **Note** > > This configuration creates the computed field on indexing. > **Important** > > Don't use special characters in computed field names. . (Optional) Locate the `` element. ```xml ``` . (Optional) Add a new `` element as an immediate child of the `` element. ```xml ``` Replace the `fieldName` attribute value with the name you chose earlier for your Coveo index field. Replace the `returnType` attribute value with the [appropriate value](https://docs.coveo.com/en/2562#fieldnames) in the context. > **Note** > > This configuration lets you specify special settings for the new field (for example, so that you may use the field as a facet or to sort search results). ## Step 3: Rebuild your search indexes [Rebuild your search indexes](https://docs.coveo.com/en/2426#rebuilding-a-search-index-manually), or at least [re-index the Sitecore items which need the new field](https://docs.coveo.com/en/2426#re-indexing-a-section-of-your-content-tree-in-the-content-editor). ## Step 4: Validate the computed field has been added Validate that your new field has been added on your items using the [**Content Browser**](https://platform.cloud.coveo.com/admin/#/orgid/content/browser/) ([platform-ca](https://platform-ca.cloud.coveo.com/admin/#/orgid/content/browser/) | [platform-eu](https://platform-eu.cloud.coveo.com/admin/#/orgid/content/browser/) | [platform-au](https://platform-au.cloud.coveo.com/admin/#/orgid/content/browser/)). > **Warning** > > When running computed field code, check your logs for computed field errors indicating items aren't being indexed. > Implement proper exception handling to avoid these issues. > For example, you might want to stop the process or set a fallback value for a computed field when suitable. ## Step 5: Use your new field Use the new field in whatever way is needed. For example, you may want to [display it in a result template](https://docs.coveo.com/en/2834#adding-conditions-and-fields-to-a-result-template).