Gravatar for emil.christiansen@kraftvaerk.com

Question by emil_christiansen, Mar 30, 2016 2:17 AM

Coveo For Sitecore - Change "Search page item" from code

Hi there.

We are running a multisite setup in Sitecore (version Sitecore.NET 8.0 (rev. 150427)) with Coveo For Sitecore installed.

The search is up and running - and we can search the two different sites just fine.

The problem is that if you are using the 'Coveo Search Box' you are allways redirected to the search page on 'site A' even though you are searchinh using the search box on 'site B'

We know that you can choose the 'Search page item' on the 'Coveo Search Box' - but the problem is that the standard value always is to the 'site A' search page - and we don't want to change it manually for every page (and new created pages) on the 'site B'.

Is it in anywaypossible to change the 'Search page item' (search page url) from the code:

In the SearchBoxView.cshtml file we currently have this:

Coveo.$(function () {
    if (typeof(CoveoForSitecore) !== 'undefined') {
        CoveoForSitecore.componentsOptions = @(Html.Raw(Model.GetJavaScriptInitializationOptions()))

        Coveo.$('#@Model.SearchBoxUniqueId')
        .on("buildingQuery", function(e, args) {
            args.queryBuilder.constantExpression.add('(@Model.ToCoveoFieldName("haslayout")) == "1" AND (@Model.ToCoveoFieldName("customsite")) == @currentSite');
        })
        .coveoForSitecore('initSearchBox',CoveoForSitecore.componentsOptions);
    } else {
        Coveo.$('#@Model.SearchBoxUniqueId').coveo('initSearchBox',
                                                   '@Model.GetSearchPageUrl()');
    }
});

And i guess we can set the search page url in there via extending the query - but how do we do it?

Thanks

2 Replies
Gravatar for sbelzile@coveo.com

Answer by Sébastien Belzile, Mar 30, 2016 8:24 AM

I see 2 possible solutions to your problem.

  1. via JS: replace the search page url by whatever fits with the context of your site.

Ex:

var url = x;
var hostName = window.location.hostname;
if (hostName.StartsWith(site1Url)) {
    url = y;
}
Coveo.$('#@Model.SearchBoxUniqueId').coveo('initSearchBox', url);
  1. You could override the GetSearchPageUrl method of the SearchboxModel class and compute the URL using the SitecoreContext (which is a protected property of the Model).
Gravatar for emil.christiansen@kraftvaerk.com

Comment by emil_christiansen, Mar 30, 2016 9:21 AM

Thanks! I will try it out.

Have one more quiestion - if i still want to use the Model.GetJavaScriptInitializationOptions() but only override the search url, how do i do that?

I mean - where do i put the code in this block?:

if (typeof(CoveoForSitecore) !== 'undefined') {
    CoveoForSitecore.componentsOptions = @(Html.Raw(Model.GetJavaScriptInitializationOptions()))
    Coveo.$('#@Model.SearchBoxUniqueId')
    .on("buildingQuery", function(e, args) {
        args.queryBuilder.constantExpression.add('(@Model.ToCoveoFieldName("haslayout")) == "1" AND (@Model.ToCoveoFieldName("customsite")) == @currentSite');
    })
    .coveoForSitecore('initSearchBox',CoveoForSitecore.componentsOptions);
}
Gravatar for sbelzile@coveo.com

Comment by Sébastien Belzile, Mar 30, 2016 9:29 AM

Under the option initialization :

CoveoForSitecore.componentsOptions = <%= Model.GetJavaScriptInitializationOptions() %>;
CoveoForSitecore.componentsOptions.searchRedirectionUrl = [An URL];

But I think Jeff's answers would be something to try. The demo searchbox does what you want OOTB. You could simply copy-paste-adapt his code sample.

Gravatar for jflheureux@coveo.com

Answer by Jean-François L'Heureux, Mar 30, 2016 8:38 AM

The Coveo for Launch Sitecore demo package does exactly what you are looking for. It does this by creating a custom data template and a custom sublayout (you can use a view too) that doesn't require a "Search Page Item" in the configuration. A custom model class is also used in the sublayout code. The model GetSearchPageUrl() method has a custom implementation that relies on a method to get the current site home item and appends "/CoveoSearch" to its URL. Download the demo package and install it to fully understand how it is built. Here's the core of the code:

public class LaunchSearchBoxModel : SearchBoxModel, ILaunchSearchBoxModel
{
    private const string COVEO_SEARCH_PAGE_ITEM_PATH_FORMAT = "{0}/CoveoSearch";

    private readonly IUrlUtilities m_UrlUtilities;

    ...

    public override string GetSearchPageUrl()
    {
        string url;

        Item searchPageItem = GetCurrentSiteSearchPageItem();
        if (searchPageItem != null) {
            IItem searchPageItemWrapper = new ItemWrapper(searchPageItem);
            url = GetItemUrl(searchPageItemWrapper);
        } else {
            url = base.GetSearchPageUrl();
        }

        return url;
    }

    private Item GetCurrentSiteSearchPageItem()
    {
        return SitecoreContext.GetCurrentDatabase().SitecoreDatabase.GetItem(GetCurrentSiteSearchPageItemPath());
    }

    private static string GetCurrentSiteSearchPageItemPath()
    {
        return string.Format(COVEO_SEARCH_PAGE_ITEM_PATH_FORMAT, SiteConfiguration.GetHomeItem().Paths.FullPath);
    }

    private string GetItemUrl(IItem p_Item)
    {
        return m_UrlUtilities.GetItemUrl(p_Item, m_UrlUtilities.GetDefaultUrlOptions());
    }
}
Ask a question