Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / SharePoint

Customizing SharePoint 2013 Search Center

5.00/5 (1 vote)
27 Jan 2016CPOL8 min read 14.1K  
How to customize SharePoint 2013 Search Center

In SharePoint 2010, FAST search existed alongside with SharePoint Search. In SharePoint 2013, both have been integrated and you now have a single interface of SharePoint Search.

In this post, we’ll go over some SharePoint 2013 Search concepts such as crawled properties, managed properties. Then, we’ll see how you can customize the search center with Custom result page and navigation, Result Source, Display template, Result type, Query Rules and Refiners.

In SharePoint online, search center should be already there. Otherwise, you can create a site collection and select the template as ‘Enterprise Search Center’.

Search Center Creation

If you navigate to the search center and search for a term, you will get the results formatted in a standard way. If you hover on the result items, you will get a flyout with some details. On the left, you will have the option to filter the results.

Below the search box (in the results page), you will have the options to search for specific type of content such as People, Conversations, Video, etc. Apart from this, you can add custom categories. In the example, we’ll go over creating a category called Books which will search for items with the ContentType of Book.

People Search

Crawled Properties

When search indexer crawls the farm, it creates a crawled property for every column. The columns which are marked as NoCrawl are excluded from this.

Managed Properties

Each Managed Property maps to one or more crawled properties. We use the managed property names in the queries, filters, etc. So, it is the interface between SharePoint and Search. In SharePoint 2013, the managed properties are also created automatically for the site columns. These are usually prefixed with OWS.

Each managed property has its own properties such as Queryable, Searchable, etc. You can find the full list at Overview of crawled and managed properties.

Customizing the Search Center

Preparation

We need some content to be able to search and display the results. For this example, I have created a content type called Book and added the columns:

  • BookAuthor – Single line of text
  • BookCategory – Single line of text

Create a custom list called Books and add the Book content type, set it as the default and remove the out of box Item content type.

In order to create or modify managed properties and crawled properties, you can navigate to the ‘search service application’ and click on ‘Search Schema’ in on-premises installation. In Office 365, you can navigate to the admin portal -> SharePoint -> Search -> Manage Search Schema.

After your content is crawled, navigate to the search schema and search for BookAuthor. You should get something like ows_BookAuthor.

Navigate to the search schema and ensure you are on the Managed properties tab on top.

Click on ‘New Managed Property’.

  1. Give a name for the property such as ‘BookAuthor
  2. Select the options Searchable, Queryable, Retrievable
  3. Click on ‘Add a Mapping’ in the Mappings to crawled properties section and add a mapping to ows_BookAuthor
  4. Click OK New Managed Property
  5. Similarly create another managed property called BookCategory and map it to ows_BookCategory

You can use the Keyword Query Language (KQL) to craft your queries. You can learn about KQL at this MSDN location. To search a specific kind of content, you can use ContentClass which is a special kind of managed property provided out of the box in SharePoint. You can read about it here. Some things to note in KQL are:

By default, if you do not specify the operator, it will assume it as AND or +

So, in the search center search box, if you give a query such as:

SQL
ContentClass:STS_List Title:SharePoint

It searches for items in custom lists (ContentClass:STS_List Title) AND Title. Notice that the space is interpreted as AND operator.

Modifying the Search Center

Create a Results Page

  1. In the Search Center, go to site contents and open the Pages library
  2. Click on New and select Page as the template
  3. Only available page layout should be the (Welcome Page) Search Results. Ensure this is selected if there are other page layouts also. Let's create a page called Books.

    Create Page

  4. Check in and publish this page.

Add the Results Page to the Navigation

  1. In the Search Center, go to the site settings page
  2. Click on Search Settings link
  3. Click on the Add Link in the ‘Configure Search Navigation’ section

    Search Navigation

  4. Add the link to the page created earlier

    Add Search Link

  5. Once it is added, you can use the Move Up, Move Down buttons to set the order of your categories
  6. Now if you navigate to the Search Center, you should see the newly created category.

    Search Center With Category

Creating the Result Source

Result source allows us to query a specific subset of the content source. Here, let’s create a result source which queries all the lists which have a content type of Book. We can define this subset using KQL.

You can create result sources at the site collection level or service application level. In Office 365, you can go to the admin center -> Search -> Manage Result Sources.

Click on ‘New result source’.

In the pop up form, you can specify a ‘Query Transform’. Modify the exisiting text to {searchTerms} ContentType:Book – This will ensure that when we search in the Book category, it searches for only items with Content type as Book. You can click on the ‘Launch Query Builder’ to build your query using UI.

Click on Save.

Setting the Result Source for the Results Web Part

  1. Navigate to the search results page we created earlier, and edit the page
  2. Edit the search results web part
  3. Click on Change Query button in the toolpartSearch Results Toolpart
  4. In the ‘select a query’ drop down, select the result source which you created earlier

    Setting the result source

  5. Click OK on the pop up and the tool part
  6. Check in and publish the page
  7. After the content is crawled, you should be able to see the items from the Books list. In my case, I have one item in my results

    Result source output

Modifying the Appearance of the Results

Display Template

  1. Navigate to the site settings(site collection)
  2. Click on ‘Master pages and page layouts’ -> ‘Display Templates’ -> Search
  3. You will notice that there are many js files. These are autogenerated by SharePoint. We need not and we should not modify these files. When we upload a new html file, the js file is automatically generated.
  4. Like many things in SharePoint, it’s a good idea to start with an existing template. So, let's download the Item_Default.html
  5. Rename the file for example to Book_Default.html
  6. The <mso:ManagedPropertyMapping tag contains the managed property mappings. If we want to use our custom managed property in the display, we need to add it here. Let's add ,’BookAuthor’:’BookAuthor’. The first is the managed property and after the : it is the alias. Let's keep the same for both.
  7. Now, you can use this in the div section below such as
    <a href="_#= ctx.CurrentItem.Path=#_" title="_#= ctx.CurrentItem.Title=#_" >_#= ctx.CurrentItem.Title=#_</a><br />
    	<span>Author: _#= ctx.CurrentItem.BookAuthor=#_</span>
  8. Now, upload the Book_Default.html to the display templates gallery (same location from where you downloaded the html)

Result Type

In order to use the display template in the search results page, you need to create a result type. And result types can be associated to a result source and we will associate with the Books result source.

  1. Go to site settings and click on Result Types
  2. Click on ‘New Result Type’
  3. In the ‘Which source should the results match’, select the Books result source created earlier Result Type
  4. Give a name and click Save

Now if you check the results page, Books category results page should reflect your formatting changesSearch Results

Query Rules

Query rules allow you to modify the users keyword search based on a condition. Let's say when the user types Developer, we want to retrieve only the books which have BookCategory as Developer and if they type ‘IT Pro’, we only want to retrieve the Administrator related books.

  1. In the search service application(on premises) or search administration(sharepoint online), click on Manage Query Rules.
  2. In the ‘Select a result source’, drop down select Books
  3. Click on ‘add a query rule’
  4. Give the name as Developer. In the conditions, select ‘Query contains action term’ -> ‘Action term contains one of these phrases and enter Developer
  5. In the Actions section, click ‘Add a result block’
  6. Configure the query to get something like {?BookCategory:{actionTerms}} . We have configured the actionTerm as Developer in this case
  7. Click on Save
  8. Create a similar query rule for IT Pro
  9. Now when you search for a term containing the action term, it shows a result block with the matching items Result Block

Refiners

On the left hand side of the results, you will have refiners for created date and modified date. This will allow you to filter the results.

You can create custom refiners by selecting Refinable when creating the managed property. Then, if you edit the results page, on the left hand there will be a Refinement web part. If you edit this web part, you will be able to select from all the managed properties which are marked as Refinable.

We saw the overview of SharePoint 2013 Search and how we can customize the Search Center using Result Sources, Display Templates, Result Types, Query Rules and Refiners.

The post Customizing SharePoint 2013 Search Center appeared first on The SharePoint Guide.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)