Wednesday, October 30, 2013

Limiting Search Results to a particular Site

To the end-user in SharePoint, the content structure is built up by Site Collections and Sites. A site collection, from the name, is a collection of sites, including one top-level site, and any number of sub-sites below.

See http://technet.microsoft.com/en-us/library/cc262410.aspx for more information on site collections and sites.

In most cases knowing this bit of information keeps you afloat, but it doesn’t take long before you try to do something not documented on office.microsoft.com, and you start seeing the word web used every now and then. You might know all of this already, but for reference to the unknowing here are the relationships:

Office.com documentation MSDN documentation
Site Collection SPSite
Site SPWeb

Being a developer I’m used to this and can quickly jump between the red and blue pill worlds, knowing when a Site means a site collection and when it means a single site (web).

And this is where the worlds collide this time in terms of Query Rules.

 

The managed property to limit results for a particular site is named WebId, derived from the SPWeb.ID property. The Query Rule variable for the same id is named Site.ID. The query rule will end up looking like:

{searchTerms} WebId:{Site.ID}

Not exactly intuitive to the common user, but it is what it is.

The Use Case

I’m in the process of creating a site template which includes a  custom search page. This page by default should be limited to searching for content within one particular site (or SPWeb).

To illustrate this I have one top site at http://contoso.com/projects, and below this top site I have http://contoso.com/projects/project1 and http://contoso.com/projects/project2. When provisioning project3 I want the search page to limit results to http://contoso.com/projects/project3. So, how do you achieve this?

There’s a couple of steps involved:

  1. Create a dedicated result source for the custom search page
  2. Create a query rule tied to the above result source, which limits result to the current site (office.com notation)
  3. Configure the search page to use the dedicated result source

Coupling the query rule and result source is necessary to avoid the query rule to trigger on regular searches on any other search page.

The result source and query rule has to be created on the site collection level (or SSA level), and not the site level. The reason being when you save a site as a template,  search settings are not included. Creating them on the site collection allows new sites created by the template to automatically pick up and use them, as the sites reside in the same site collection.

First create a new Result Source named SubSiteSearch using “Local SharePoint” as the source (on the site collection or SSA level).

image

Next create a Query rule which triggers for the above Result Source.

image

Remove the trigger condition to have the rule trigger on all searches and add the Query Rule as mentioned above: {searchTerms} WebId:{Site.ID}

image

The last part is creating the search page and configuring it to use the SubSiteSearch result source. For my template I’m using a web part page with a Search Result web part.

image

The results looks similar to the image below, limited to subsite1

image