4

I'm currently working on SXA OOTB the Search Box and Search Result rendering.

SXA sites structure is as below

Sites

  • Country Site 1

  • Country Site 2

  • Shared Site

I have search box in the site header which is partial design created in shared site for reuse purpose.

When I'm trying to configure Search Result Page property on my Search Box in the header it is displaying the pages from the shared website and not from the Country Website1. There is no Search result page in shared website.

enter image description here

When I go to the Search Result page on my country site and enter the query in Search box, everything is working fine. However if I'm on different page and type query in search box it doesn't redirect me to the search result page.

1
  • 1
    I will confirm it but this might be a bug. Give me a day, I need to find a time and I will be back to you. Commented Dec 10, 2019 at 7:25

1 Answer 1

6

I had the same issue and was not able to resolve this with OOTB functionality. You could change the source field (query:$home) of the OOTB Search Box Template (/sitecore/templates/Feature/Experience Accelerator/Search/Rendering Parameters/Search Box/SearchCriteria/SearchResultPage) but this is not a recommended approach.

Therefore, I created my own rendering and inherited from the SearchBoxRepository class. By overriding the GetSearchResultPageUrl() method I was able to get my site specific Search Results Page while the rendering was placed in the header (in a partial design in the shared site).

1. Create a feature project in your solution which contains a controller and a repository.

public class MultiSiteSearchBoxController : StandardController
{
   private readonly IMultiSiteSearchBoxRepository _repository;

   public MultiSiteSearchBoxController(IMultiSiteSearchBoxRepository repository)
   {
      _repository = repository;
   }

   protected override object GetModel()
   {
      return _repository.GetModel();
   }
}
public interface IMultiSiteSearchBoxRepository : IModelRepository
{
}
[Service(typeof(IMultiSiteSearchBoxRepository), Lifetime = Lifetime.Transient)]
public class MultiSiteSearchBoxRepository : SearchBoxRepository, IMultiSiteSearchBoxRepository
{
   public MultiSiteSearchBoxRepository(ISiteInfoResolver siteInfoResolver, IVariantsRepository variantsRepository, IScopeService scopeService) 
            :base(siteInfoResolver, variantsRepository, scopeService)
   {
   }

   public override IRenderingModelBase GetModel()
   {
      SearchBoxRenderingModel model = (SearchBoxRenderingModel)base.GetModel();
      return model;
   }

   protected override string GetSearchResultPageUrl()
   {            
      // get your settings item based on the current context (country site 1 or country site 2)
      var multiSiteSearchBoxSettings = dosomething_to_get_your_country_site_searchbox_settings();
      if (multiSiteSearchBoxSettings == null)
      {
         return base.GetSearchResultPageUrl();
      }

      // now lets get the page you have set inside your country site 1 or country site 2 settings item
      var searchResultsPagePath = LinkManager.GetItemUrl(Sitecore.Context.Database.GetItem(multiSiteSearchBoxSettings[The_ID_Of_Your_SearchResultsPage_Field]));

      return string.IsNullOrEmpty(searchResultsPagePath) ? base.GetSearchResultPageUrl() : searchResultsPagePath;
   }
}

2. Create the rendering

Basically, it's a clone of the OOTB Search Box rendering (/sitecore/layout/Renderings/Feature/Experience Accelerator/Search/Search Box) except it's linked to our own controller.

Controller rendering Parameters Template Datasource View Path

3. Create the settings template which holds the link to your site specific Search Results Page Settings template

4. Add settings item (based on the latter template) to each Country site Settings Item

4. Finally, add the Multi Site Search Box rendering to your partial design in the Shared site

Just leave the Search Result Page field blank since we will get our own url by overriding the GetSearchResultPageUrl() method. Add rendering

3
  • Well explained Mathijs. I have implemented the OOTB approach before, will definitely try this approach in future.
    – D J
    Commented Dec 11, 2019 at 12:05
  • Thank you very much Mathijis! I will implement with the recommended approach. Commented Dec 12, 2019 at 1:00
  • 2
    @MohitDharmadhikari Were you able to resolve your issue using my apporach? If so, could you please accept it as the answer? Commented Dec 18, 2019 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.