How To Create A Custom Episerver Editor Search Provider

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.  In Episerver, 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.

custom_editor_search

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.

Note If you need information on how to set-up the search have a look at How To Set-up and Install The Episerver Search

PageSearchProvider

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:

[SearchProvider]
public class InternalPageSearchProvider : PageSearchProvider
{
public InternalPageSearchProvider(LocalizationService localizationService,
SiteDefinitionResolver siteDefinitionResolver,
PageTypeRepository contentTypeRepository,
IContentRepository contentRepository,
ILanguageBranchRepository languageBranchRepository,
SearchHandler searchHandler,
ContentSearchHandler contentSearchHandler,
SearchIndexConfig searchIndexConfig)
: base(localizationService, siteDefinitionResolver, contentTypeRepository, contentRepository, languageBranchRepository, searchHandler, contentSearchHandler, searchIndexConfig)
{
}
public override IEnumerable<SearchResult> Search(Query query)
{
return base.Search(query);
}
}

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:

public override IEnumerable<SearchResult> Search(Query query)
{
query.MaxResults = 20;
if (query.SearchQuery.Contains("supplier:"))
{
query.SearchQuery = query.SearchQuery.Replace("supplier:", string.Empty);
query.SearchRoots = new[] { "33" };
}
return base.Search(query);
}

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.

In my website 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 hard coded directly.

custom_editor_search_filtered

Conclusion

In today’s guide I’ve covered the limitations of the editor search.  By inheriting from PageSearchProvider, you can hi-jack the editors 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.

Code Sample

All the above code can be downloaded in a fully working website from my github account here.

JonDJones.com.EpiserverCustomPageSearchProvider

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *