While SXA search will often meet the client's search requirements, there are some limitations with out-of-the-box implementation.
Sitecore SXA search features are really flexible and everything is content manageable. While SXA search will often meet the client search requirements, there are some limitations with the out-of-the-box implementation, i.e. the rendering template is hard-coded in the JavaScript, which can present some challenges when the front-end developers want to work with the templates.
Therefore, we want to extend the search results rendering to provide front-end flexibility, allow for mark-up changes, and also make sure the results mark-up is scalable and re-usable.
The search results rendering is one of the SXA search components. It utilises the global scope for sharing the settings between each of the search components i.e. search box, search filter, load more button etc. After the initial load of a page, the search results rendering will make an AJAX call to the search service API by passing the search scope and query. Once it gets the results from the server, it will then render them on the page with the pre-defined template. This is how the out-of-the-box search results rendering work. The approach is good, as it modularizes the component into small individual components i.e. pagination, search box, filter etc., which has great maintainability and re-usability.
The custom search results rendering described below is built on top of the out-of-the-box search results rendering, but adds some enhancements.
What we're going to be doing is :
Let's dive into the steps for extending Sitecore SXA search results below.
As when creating other custom SXA controllers, you will need to create a custom search results controller.
In order to use the variant for rendering the search results, the custom repository needs to inherit from VariantsRepository
, which is the Sitecore.XA.Foundation.RenderingVariants.Repositorie
namespace.
Also, we will need to inject IScopeService
for utilizing the global scope in the custom repository constructor as shown below.
Next we need to create a custom method for retrieving the results.
We hope you enjoyed our post about extending Sitecore SXA search results.
Subscribe for the latest news and events from our talented team and community network.