Sitefinity Asynchronous Search with WebAPI

Overview

Sitefinity CMS features a powerful internal Search Engine that allows your site visitors to search your content and find what they are looking for. The Search Engine is built on top of the .NET port of the infamous Lunene Engine which powers an unlimited number of websites today.

Unlike the solid Search Engine behind it, the Search Widget which Sitefinity offers is not as mature. It provides the options to set up a basic search page and returns the results in a form of a list. Perhaps that will be satisfactory in most cases, but what if your requirements exceed those of a simple search?. What if you’d like to create an Amazon-like search page for books or products?

It’s that sort of transition !

Game of thrones

Wait, I’ve customized Search Before

It’s possible that you have been there before, and that you have extended the Search widget with the help of great blog posts such as this one by Svetla. There is no denial that the team did a formidable job explaining how to extend and customize every part of the CMS, whether it’s through the documentation portal, blog posts, forums or several webinars. Search is still missing its asynchronous nature and there is no way that we can build our own interface for the search page while making use of a Search service which searches the appropriate index for us. Furthermore, it’s almost impossible to search Sitefinity content from another client so the idea of creating Content Driven Mobile Apps is hindered by the inadequacy of the Search Widget.

Worry not, we’ve got you covered

I put together an asynchronous implementation of the Sitefinity Search using Web API. I leveraged as much as possible the code from the Search Widget so it respects the settings of your site. It consists of one API Controller with a single action:

[HttpGet]
        public SearchResultModel Get(string searchTerm, int skip = 0, int take = 5)
        {
            var model = new SearchResultModel {Term = searchTerm};
            int hitCount;

            var results = new List<SearchDocumentModel>();
            
            Search(searchTerm, Catalogue, skip, take, out hitCount).ForEach(s => results.Add(Models.ModelFactory.Create(s)));
            
            model.Count = hitCount;
            model.Results = results;

            return model;
        }

The Search Result Model class defines the structure of the search result data:

    public class SearchResultModel
    {
        public string Term { get; set; }
        public int Count { get; set; }
        public IEnumerable<SearchDocumentModel> Results { get; set; } 
    }

The repo is hosted on Github and it will be a good starting point for you if you want to put together advanced searching capabilities on your site. You will need to follow the Installation instructions on Github. Once configured, you can start hitting the end point and searching your content. You can check out the search page that’s included under “Tests”.

Conclusion

With the help of Web API, we can rewrite our search pages (using JavaScript) and asynchronously search a Sitefinity site. This will dramatically improve the search experience on your site and eventually leads to increasing your conversion rate.

 

 

0 Comments

Leave a Reply

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