Add custom context to the Community Hosted Search Page component

In this article

To add custom context to the Coveo Community Hosted Search Page component, you must create a custom Apex class that implements CoveoV2.HostedCustomContextInterface as described in this article.

To add custom context

  1. Create an Apex class that implements CoveoV2.HostedCustomContextInterface as follows:

    global class MyCustomContext implements CoveoV2.HostedCustomContextInterface {
      public Map<String, Object> getCustomContext(Map<String, Object> data) {
        // Returns the map of custom context values that will be added to each query.
        return new Map<String, Object>{
          'key1' => 'value1', 1
          'listOfValues' => new List<String>{'listValue1', 'listValue2'} 2
        };
      }
    }
    1 key1 is the context key/value pair to add to each query.
    2 listOfValues is the array of context values to add to each query.
    Example
    global class MyCustomContext implements CoveoV2.HostedCustomContextInterface {
      public Map<String, Object> getCustomContext(Map<String, Object> data) {
        return new Map<String, Object>{
            'country' => 'Canada',
            'role' => 'Admin',
            'department' => 'IT',
            'interests' => new List<String>{'encryption', 'cloud', 'security'}
        };
      }
    }
  2. Save the Apex class.

What’s next?

Once you’ve created an Apex class that implements CoveoV2.HostedCustomContextInterface, you can select it from the Custom Context Provider picklist in the Coveo Community Hosted Search Page component to augment queries with the additional context returned by your custom Apex class.