Tuesday 15 October 2019

Override Coveo Global Search Box

In a recent project I was working on, this projtect contain up to 7 microsites, were these microsites sub trees of the content tree under the main site home item, there was a Coveo for Sitecore cloud 4 implementation, and as requested we wanted to use the same global search result, BUT when a user initiated search from any of these microsites we wanted the search result to be pre-filtered by the microsite the search initiated from, in this post I will show you how we did this.


Following how my content tree looks like:

First step we did is to tag each item under these microsites with the related microsite name, and to do that we created  index computed field called "micrositename", following is the configuration and the bacnkend code for this computed field:


In the config file Coveo.SearchProvider.Custom.config, we add the following line into the field map section:

and the following line into the computed field section:



Once you create the above you will need to rebuild your Coveo indexes, and then you can add a new facet to your search result with the name "MicrositeName", this facet will pull in the different microsite names values that have been indexed while rebuilding the indexes.


Now what we want to is to customize our global search box rendering to include and pass the microsite parameter as query string so search result can use it as pre-defined value for the microsite name filter. And to be able to do that we need to override the initialization file for the global search box, Coveo for Sitecore made this very easy, first you need to navigate to the following path:

...\Views\Coveo Hive\Search Boxes\Coveo Searchbox.cshtml

at the top of the view file you will see the initialization line where we tell the rendering where to find its initialization file, what we will do is; to change the view to point to a custom file that we will change to override the business need that we discussed earlier which is passing the microsite name attribute as parameter, see line 11 in the following snippet which replaced the following:
@Html.Coveo().InitializationPartial(Initializers.SEARCHBOX, Model)



Now, in the following path you can create the custom initialization file, you can copy and rename the original one:
\Views\Coveo Hive\Initialization\Custom Initialize Searchbox.cshtml

Following how the custom code to pass the new parameter looks like:

Hope this helps or give you idea how to overcome such scenario, if you have a better way to resolve this case please feel free to comment or send it to me by email. 


No comments:

Post a Comment