Sunday, April 27, 2014

Create custom search result source programmatically in Sharepoint 2013

If you need to customize search results on your Sharepoint 2013 site, e.g. add additional search rules which will be used with the keywords entered by the user in the search box, then you will need to create custom search result source (there are other ways to do it also, but custom result source is the most straightforward and designed exactly for such requirements). Result source may be created on site collection level and marked as default. In this case it will be used on default search results page or in content by search web parts (if another result source is not specified in web parts properties explicitly). In this post I will show how to create custom search result source programmatically during provisioning. It will help to automate installation process.

Let’s create result source which will have additional filter for the News by managed metadata property Country. I.e. we want to show only those news which are tagged for appropriate country. Here is how it can be done:

   1: string query = "{{?{{searchTerms}} -ContentClass=urn:content-class:SPSPeople " +
   2:     "(ContentType<>News OR (ContentType=News AND Countries:\"Russia\"}}";
   4: SPSite site = ...;
   5: var searchAppProxy = getSearchServiceApplicationProxy(site);
   6: var federationManager = new FederationManager(searchAppProxy);
   7: var searchOwner = new SearchObjectOwner(SearchObjectLevel.SPSite, site.RootWeb);
   8: var resultSource = federationManager.CreateSource(searchOwner);
   9: resultSource.Name = "Localized Search Results";
  10: resultSource.ProviderId =
  11:     federationManager.ListProviders()["Local SharePoint Provider"].Id;
  12: resultSource.CreateQueryTransform(new QueryTransformProperties(), query);
  13: resultSource.Commit();
  14: federationManager.UpdateDefaultSource(resultSource.Id, searchOwner);

Here is the parts included to our query:

?{searchTerms} – placeholder for the keywords entered by the user in search box;
-ContentClass=urn:content-class:SPSPeople – exclude people from search result;
(ContentType<>News OR (ContentType=News AND Countries:"Russia"} – show news which are tagged for Russia. Other content is shown regardless of the country.

These rules are combined by space which means AND operator in KQL. As result only those content will be shown in the search results which matches to all rules.

Note that on line 14 we also set created result source as default (federationManager.UpdateDefaultSource(…)). Note that after that catalog connections will disappear in Site settings > Manage catalog connections > Connect to a catalog and you won’t be able to connect to publishing catalog. I wrote about this problem in Problem with missing catalog connections when use customized search result source in Sharepoint 2013 article. Workaround is to set OTB result source “Local SharePoint Results” as default temporary, connect to catalog and set custom result source back to default.

No comments:

Post a Comment