On a recent project, we were migrating an old system that was built on a platform called Django into Episerver. As part of the migration, a lot of the old systems admin pages were converted into Episerver as custom metadata pages, some rough examples are things like suppliers, comments and inventory.
One of the big differences between the two systems is that in Django you can create a separate page for each type of metadata for content editors to use. The benefit of this approach is that at the top of each page in Django, a search box was included on the page that restricted the search to only return results relevant to that page. In Episerver, because the data got imported as pages, the content team missed this filtered search feature.
If you use the out of the box search provider, it will return all pages in the CMS (limited to 5 results) you get everything returned when you search. The most annoying part is that the page type is not returned in the display. If, for example, you have 10 pages all called the same thing, but live in different areas in the content hierarchy, it's really hard for content editors to know which is the search result they want to use.
Luckily, in Episerver we can override the default search provider and customise it. In this guide, we're going to do just that. We're going to add shortcut codes inside the search engine, so, if someone typed: supplier:page name Only pages within the supplier section in the CMS are returned in the search results.
If you need information on how to set-up the search have a look at How To Set-up and Install The Episerver Search
In order to override the default search provider, we need to create a class that inherits from 'PageSearchProvider'. When we do this, we also need to implement the PageSearchProvider constructor:
We now have a class that will hijack any search that a content editor makes within the CMS. The next part is to configure it to filter results based on a short-cut code:
The first thing I'm doing is changing the maximum number the search returns from 5 to 20. The next thing I'm doing is checking if the query made by the user containers the short-code identifier "supplier:". If the content editor has searched for this term, then I'm restricting the area the search engine will query.
I have an area called 'Supplier' that has a page ID of 33. In Search roots, I can then pass in this page's page id (as a string) to restrict the search to only children of page 33. In production code, I would use a settings helper or something similar to pass in the page id rather than just hardcoded directly.
In today's guide, I've covered the limitations of the editor search. By inheriting from PageSearchProvider, you can hi-jack the editor's requests and filter the search results. One benefit of this is by introducing short-cut codes to filter the location of the search to a specific area. I added a shortcut code called "Supplier:" to only return pages from a certain section of the editor. This was done using the SearchRoots property.
All the above code can be downloaded in a fully working website from my GitHub account here. JonDJones.com.EpiserverCustomPageSearchProvider