BAt its most basic, building a site search in SXA requires two components: a search box and a search results page. The goal of the task detailed here is that the user is able to select a “Search” module when creating a new SXA site, and these components will automatically populate the site.

Let’s say the search box will live on the Footer partial design, to be included in every page design. Open up the Footer partial design and add “Search Box” from under the Search tab. Clicking “Edit Component Properties” will reveal some important details that need to be set.

Leave “Search scope” blank to allow the search to apply to the entire site. While some other properties can be left blank for now, “Search Result Page” must be filled out.

For this, we will want to create another page on our site we can call “Search” and point this property to that page. “Search Results” should be added as a rendering to this page. With that, we have a very simple site search set up.

To configure the module, right click /sitecore/system/Settings/Feature and add a Module. Only the Settings and Branches system areas need to be checked. Navigate to the templates’ Branches folder and add a new branch to contain the Search page, and another the Footer partial design.

At this point, we have a major problem creating the Footer page design. First, you cannot use Experience Editor to build out the partial design from a branch. The easiest solution is to copy the Footer from your site to this branch. That way it will have all the presentation details it needs without having to set them up.

What about the Search Results Page, though? Right now the search box points to your own site’s Search page. It needs to point to the new site’s search page, but there’s no way to know that path yet.

I had to write a Powershell script to get around this issue. It looks up the site’s search page, and then replaces the GUID within the Footer’s Renderings.

Import-Function Get-SiteItem

$SiteItem = Get-SiteItem $Site

#Get footer

$footer = Get-Item -Path “master://$($SiteItem.Paths.Path)/Presentation/Partial Designs/Footer”

#Get search page

$searchPage = Get-Item -Path “master://$($SiteItem.Paths.Path)/Home/Search”

#url encode search page ID

$searchPageID = [System.Web.HttpUtility]::UrlEncode($searchPage.ID)

#text replace footer Renderings

$footer.Locking.Unlock()

$footer.Editing.BeginEdit()

$footer.Fields[‘__Renderings’].Value = $footer.Fields[‘__Renderings’].Value.replace(“SearchResultPage=%7b74A89460-3E7A-4A64-887A-2D766706ACAE%7d&”, “SearchResultPage=$searchPageID&”)

$footer.Editing.EndEdit()

Your value to replace is going to include a different GUID. Check the raw value of the Renderings under /sitecore/content/{Your SXA Sites}/{Your Site Name}/Presentation/Partial Designs/Footer.

This script is added to the module’s SiteSetupRoot in Settings by inserting “Execute Site Script”.

Building a SXA Site Search Module

Leave a Reply

Your email address will not be published.