Dynamically linking Coveo Search box to target search page


 

 

 

You will need to specify the "search page item" for each an every of those sites, either at template standard value level or individual item level. What if you are using the same template across sites and you need to specify the "search page item" at this template's standard value level.

I am going to describe a way this can be achieved at the code level, regardless of whatever you specify at the presentation settings (for "search page item" field).

1- Open the actual rendering code file "searchboxview.cshtml", for the search box.

2- Right at the beginning add these lines to calculate the "searchitempath" to be used.

@{ 
    string _searchitempath = Sitecore.Context.Site.RootPath + Sitecore.Context.Site.StartItem + "/[Your search item]";
    Sitecore.Data.Items.Item _searchItem = Sitecore.Context.Database.GetItem(_searchitempath);
    _searchitempath = Sitecore.Links.LinkManager.GetItemUrl(_searchItem);
}

3- Within the coveo script function (inside a javascript block) add the lines of code to replace the value read from "search page item":

        Coveo.$(function () {
        
        ...any other code you might have...
        
            var searchbox = Coveo.$('#@Model.SearchboxId');
            
            @* Comment or remove this original code
                searchbox.coveo('initSearchbox',
                                '@Model.GetSearchPageUrl()');*@
             
            searchbox.coveo('initSearchbox', '@_searchitempath');
        });

Here, as you can see, I have commented out the original code and replaced it with my code to pass the calculated "searchitempath" instead.

4- Save this file and now you should be able to see the correct search page for each and every site of yours.

 

 

Categories: Coveo, Sitecore

SEARCH ARTICLES