Kentico CMS 7.0 Developer's Guide

Custom SQL search provider

Custom SQL search provider

Previous topic Next topic Mail us feedback on this topic!  

Custom SQL search provider

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Kentico CMS allows you to write your own SQL search provider, which you can use to integrate a third-party search engine. You can also create a custom search provider if you need to modify or filter search results returned by the standard search engine.


You cannot customize the search provider in the App_Code folder. The modifications must be added as part of a new assembly, which you then need to register via the CMSSearchProviderAssembly web.config key.







This provider only affects the SQL Search engine. The Smart Search engine remains unchanged.


To modify the smart search, create and use a custom search index as described in Modules -> Smart Search -> Managing indexes -> Defining custom index content.



The following example demonstrates how to customize the SQL search provider so that it only searches a limited section of the website.


Adding the custom search provider assembly


1. Copy the CustomSearchProvider project from your Kentico CMS installation directory (typically C:\Program Files\KenticoCMS\<version>\CodeSamples\CustomSearchProvider) to a development folder.


2. Open your CMS web project in Visual Studio through the WebProject.sln file (or WebApp.sln if you are using a web application installation).


3. Click File -> Add -> Existing Project and select CustomSearchProvider.csproj in the folder where you copied the CustomSearchProvider project.




4. Unfold the References section of the CustomSearchProvider project and delete all invalid references.


5. Right-click the CustomSearchProvider project and select Add Reference.


6. Open the Browse tab of the Add Reference dialog and navigate to the bin folder of your CMS web project on the disk.


7. Add references to the following libraries:





8. Right‑click the CMS website object (or the CMSApp project if your installation is a web application) and select Add Reference.


9. Open the Projects tab and add a reference to the CustomSearchProvider project.


The CustomSearchProvider project is now integrated into your application.


Modifying the search provider class


1. Edit the SearchProvider.cs class in the CustomSearchProvider project.


2. Modify the code of the Search method according to the following:




public DataSet Search(string siteName, string searchNodePath, string cultureCode, string searchExpression, SearchModeEnum searchMode, bool searchChildNodes, string classNames, bool filterResultsByReadPermission, bool searchOnlyPublished, string whereCondition, string orderBy, bool combineWithDefaultCulture)
  // Limits the search path to only the /Products section of the website
   searchNodePath = "/Products/%";

  // Gets a new instance of the SQL SearchProvider
   CMS.SearchProviderSQL.SearchProvider limitedSearchProvider = new CMS.SearchProviderSQL.SearchProvider();

  // Gets the search result dataset based on the parameters of the Search method and the limited search path
  DataSet searchResults = limitedSearchProvider.Search(siteName, searchNodePath, cultureCode, searchExpression, searchMode, searchChildNodes, classNames, filterResultsByReadPermission, searchOnlyPublished, whereCondition, orderBy, combineWithDefaultCulture);
  return searchResults;


In real customization scenarios, you can modify the Search method to behave according to any requirements. The method must always return a DataSet containing the search results.


3. Save the file and Build the CustomSearchProvider project.


Registering the custom search provider


1. Edit your application's web.config file.


2. Add the following key to the configuration/appSettings section:

<add key="CMSSearchProviderAssembly" value="CMS.CustomSearchProvider" />


The system loads the SearchProvider class from the namespace specified as the value of the CMSSearchProviderAssembly key.




To try out the customized search engine, open the live website and search for text using the SQL Search engine. For example, you can use the Examples -> Web Parts -> Full‑Text Search -> SQL Search -> SQL Search dialog with results page on the sample Corporate site website.


The search now only returns results from the /Products section of the website.







This customization example is purely demonstrational. If you need to change the search path, directly set the Path property of individual SQL search web parts or controls.