Wednesday, July 10, 2013

Setup Multiple Search Pages & Result Sources (Search Scopes) for a Site Search in SharePoint 2013

When using an Enterprise Search Center in SharePoint you get all the additional search pages (scopes) out of the box, like Everything, People, Conversations, etc.  This is a very impressive search site with a lot of functionality created for you.  However, what if you don't want to use an Enterprise Search Center and all you need is the basic site search?  Luckily you can get the same experience with just a little configuration using search pages and result sources.

First, let's take a look at the site collection I've created for this demo.  Its just a simple team site with a couple of lists and a document library, nothing fancy.  I've added a few list items and a couple of documents so that the search will return some results.

Performing a search on "Item 1" returns 11 results in this case with a mix of list items, documents, and web pages.  Now, let's say we want to include a search page that only returns list items.  We can easily do this using a custom result source and a new search page. 

Let's start by creating the result source.  Navigate to the Site Settings page, and under the "Search" heading click "Result Sources".

Since we are creating this new result source at the site level, it will only be available for this site.  If we needed something for the entire site collection, you would choose "Search Result Sources" under the "Site Collection Administration" heading.

On the "Manage Result Sources" page you will see all the pre-defined result sources that come out of the box as well as any user defined ones.  Click the "New Result Source" link to create a new one.

On the "Add Result Source" page we'll need to supply some information.  First give it a name and a description, in this case "List Items" and "Only returns list items".  We also need to specify a protocol, in this case "Local SharePoint" as we want to use the SharePoint search results.

Next, we specify the type which is "SharePoint Search Results".  Jumping down to the end we have to specify the "Credentials Information" which in this case will just be "Default Authentication".

In the middle is the "Query Transform", and this is where the magic happens.  When you first come to this page you'll see this textbox only has "{searchTerms}" in it.  This is just a place holder and will be replaced with the actual search terms the user wants to search for.  We'll add two additional pieces of text to get our desired results:
  • contentClass:STS_ListItem - This property restrictor will only return items that are custom list items.  In this example since I have built a couple of custom lists and I only want to return items from these lists.
  • IsDocument:0 - This property restrictor will limit results to only items that are not documents.  If we placed a 1 instead of a 0 we would only get documents.
Combining these two property restrictors will return only custom list items that are not documents.  The final string for the textbox is:
  • {searchTerms} contentClass:STS_ListItem IsDocument:0
You can view more content class restrictors here:

Click "Save" and you'll now see the new result source in the previous list.

Now that we have our new custom result source, we need to apply it to a search page.  Looking back at the site search page you'll notice a couple of things.  First, you can not edit the page!

Looking at the address bar in the browser, you can see this page is coming from the Layouts folder.

The search page is the "osssearchresults.aspx" page that may look familiar to anyone using SharePoint.  So we need to add some other search pages that we can edit.  Luckily there are some search pages already in the environment we can use.  Navigate to the "Site Settings" page and under the "Web Designer Galleries" heading, click the "Master pages" link.

In the "Master Page Gallery" you'll see a lot of pages here and also some folders.  For this example we're looking for the "SearchResults.aspx" page located near the bottom.

This page is a standard search results page that looks and acts just like the "osssearchresults.aspx" page, but with one major difference.  We can edit it!  However, we don't want to edit this particular one.  We'll make a copy of it in SharePoint Designer to use the custom result source with.  Open SharePoint Designer and navigate to the "Master Pages" area in the left hand navigation.

Highlight the page titled "SearchResults.aspx", then from the ribbon choose "Copy".

Once the copy has been created, highlight it and choose "Rename" from the ribbon.  In this example we'll call it "SearchResultsListItems.aspx".  Now we have two search results pages in the Master Page Gallery.

Its very important to note that this is the only way to copy these files without getting errors.  If you were to download a copy, then upload it you would get errors about code blocks on the page.  Using Designer to copy it ensures you can bypass these errors.

Now that we have our copied search results page, we're ready to add it to our site search and configure it to use the result source.  Navigate to the "Site Settings" page again, and under the "Search" heading click "Search Settings".

On the "Search Settings" page we'll be working in the "Configure Search Navigation" section.  In this area we can add links that will show up under the search box on the search results page.  These links will act just like search scopes did in SharePoint 2010 once we have everything setup.

To start we have to add a link to a default search page.  The first link you add will automatically become the default search result page.  This is important to note, since if you plan on using pages with custom result sources that limit search results, you may not want these to be the default search.

Click the "Add Link" button to add in the first page.  Since we already know we have a default search results page in the master page gallery, we'll use that as the default search page.  The URL format will be "/sites/blogexamples/_catalogs/masterpage/SearchResults.aspx".

Click "OK" to add the link.

You can see the link has been added, and you can highlight it to see the details.  Click "Add Link" again, to add the second page.  Use the same URL path, but with the new page name: "/sites/blogexamples/_catalogs/masterpage/SearchResultsListItems.aspx".  Click "OK" to see the second link has been added.

Click "OK" on the search settings page to save the work.  Now go perform a search.

You can now see we have two pages available for searching!  Now you're basic site search is starting to act like an Enterprise Search Center with scopes!  Pretty cool!  However, as it stands right now both pages return the same results.  So let's edit the "List Items" page to use the custom result source.

Click the "List Items" link to navigate to that page.  Then from the page menu, you'll see we have an "Edit Page" option.

Click "Edit Page" to place the page into edit mode.  On the "Search Results" web part, choose "Edit Web Part" from the context menu.

From the web part properties window, click the "Change Query" button.

In the "Build Your Query" modal, the first section is called "Select a Query".  There is a drop down list that shows all the result sources you can choose from.

The custom result source we created earlier is listed here, called "List Items".  Select it from the drop down and click "OK" to save the settings.  Click "OK" in the web part properties and choose "Stop Editing" from the ribbon.  The search results web part is now configured to use the custom result source.  Performing another search will show the filtered results on the new page.

In my environment, searching the term "Item 1" on the main search page returns about 11 items:

However, clicking the "List Items" page, which is using the custom result source to limit results, I only get 3 items since the result source only returns list items that are not documents:

And that's it!  Now we have a basic site search that acts more like an Enterprise Search Center using custom search pages, custom result sources (search scopes), and links to each page at the top.

You can easily expand this example with more pages, more custom result sources, and even incorporate some Display Templates and Query Rules to get even more fancy!



  1. Scientific research shows that we're all born either extroverts or introverts, and that doesn't change very much as we go through life. But, no matter if you're a born extrovert or introvert we can all do certain things that will enhance our The Celeb Net Worth in real life and on the internet. When you have a wider pool of human resources in your life, you experience increased happiness, health, relationships and even financial opportunities.