March 2013: Creating customized search results in SharePoint 2013

With the launch of SharePoint 2013 came some exciting new features and improvements in the realm of Web Content Management (WCM). Content authoring improvements like Image Renditions and the ability to drag and drop navigation items directly onto a page create a better experience for content authors, while branding advancements like the new Design Manager and Device Channels further empower web designers. Throw in Managed Navigation, Cross-site Publishing, Catalog-enabled lists and libraries, Category pages, clean URLs and the ability to capture a video thumbnail straight from the video and it is clear that WCM has received quite the facelift courtesy of SharePoint 2013.

But some of the most exciting advancements have come in the area of search, including user interface improvements such as the hover panel to preview content, relevance improvements through analytics, continuous crawl which eliminates the need for scheduled incremental crawls, a more flexible search schema, and the new Content Search Web Part.

Today we are going to take a look at how to implement some of the search improvements to give our users results which are more relevant to what they are most likely searching for. In this example, we are targeting a custom page type called Story. For all Story pages that are returned in the search results, we will apply custom styling and push the items to the top of the search results. The end result is seen below.

image1

Before beginning, we created a custom page content type based off of the Article Page content type and added the following custom site column:

Now, let’s get started.

Step 1 – Display Template

The display template defines what we want our results to look like.

  1. From the Site Settings page, select Master pages and page layouts from the Web Designer Galleries section
  2. Select Display Templates and then Search
  3. Locate Item_Default.html and create a copy by selecting Download a Copy from the item dropdown menu.
    image2
  4. Save the file to you desktop and rename to Item_Story.html.
  5. Upload the file by selecting Upload Document from the Files tab in the ribbon, fill in the following fields, and then save:
    1. Content Type: Item Display Template
    2. Title (this is what shows up in all dropdowns): Story
    3. Description
    4. Everything else can stay as default values

    NOTE: Don’t create a copy of Item_Default.js. SharePoint automatically creates the corresponding .js file for you.

  6. Open site in SharePoint Designer 2013 and navigate to All Files > _catalogs > masterpage > Display Templates > Searchimage3
    image4
  7. Select Item_Story.html from the list and then select Edit file.
  8. Add additional fields to be displayed (Comments and PublishingRollupImage) as follows:

    <mso:ManagedPropertyMapping msdt:dt=”string”>’Title’:'Title’,'Path’:'Path’,'Description’:'Description’,
    ‘EditorOWSUSER’:'EditorOWSUSER’,'LastModifiedTime’:
    ‘LastModifiedTime’,'CollapsingStatus’:'CollapsingStatus’,
    ‘DocId’:'DocId’,'HitHighlightedSummary’:
    ‘HitHighlightedSummary’,'HitHighlightedProperties’:
    ‘HitHighlightedProperties’,'FileExtension’:'FileExtension’,
    ‘ViewsLifeTime’:'ViewsLifeTime’,'ParentLink’:'ParentLink’,
    ‘FileType’:'FileType’,'IsContainer’:'IsContainer’,
    ‘SecondaryFileExtension’:'SecondaryFileExtension’,'DisplayAuthor’
    :’DisplayAuthor’,‘Comments’:'Comments’,
    ‘PublishingRollupImage’:'PublishingRollupImage’
    </mso:ManagedPropertyMapping>

  9. Update HTML as follows:
    <div id=”_#= $htmlEncode(itemId) =#_” name=”Item” data-displaytemplate=”DefaultItem” class=”ms-srch-item” onmouseover=”_#= ctx.currentItem_ShowHoverPanelCallback =#_” onmouseout=”_#= ctx.currentItem_HideHoverPanelCallback =#_”>

    <table>

    <tr>

    <td class=”storySearchTitle”>
    <a href=”_#=ctx.CurrentItem.Path=#_”>
    _#=ctx.CurrentItem.Title=#_
    </a>
    </td>

    <td class=”storySearchImage” rowspan=”2″>
    _#=ctx.CurrentItem.PublishingRollupImage=#_
    </td>

    </tr>

    <tr>

    <td class=”storySearchSummary”>
    _#=ctx.CurrentItem.Comments=#_
    </td>

    </tr>

    </table>

    <div id=”_#= $htmlEncode(hoverId) =#_” class=”ms-srch-hover-outerContainer”></div>
    </div>

  10. Save changes to Item_Story.html.
  11. Create a custom CSS file called StorySearchDisplayTemplate.css and add the following styles:.storySearchTitle{
    font-family:Arial, Helvetica, sans-serif;
    font-size:12pt;
    font-weight:bold;
    }
    .storySearchSummary {
    font-family:Arial, Helvetica, sans-serif;
    }
    .storySearchImage {
    float:right;
    max-height:120px;
    max-width:120px;
    }
  12. Add a link to StorySearchDisplayTemplate.css in the <head> of Item_Story.html.
  13. Save, check in, and publish all files as necessary.

Step 2 – Result Type

The result type maps our new display template to a specific type of content.

  1. From the Site Settings page, select Result Types from the Search section.
    image5
  2. Click the New Result Type link at the top of the page and fill in the following fields:image6
    1. Name: Story
    2. Conditions:
      1. Which source should results match? Local SP results
    3. Click Show more conditions
      1. Which custom properties should match? PageTypeOWSCHCS Equals any of… Story
    4. Actions:
      1. What should these results look like? Story
  3. Click OK, and then Save

Step 3 – Query Rule

Query Rules can be used in many ways. For our example, we will create a promoted result block to push all Stories to the top of search results.

  1. From the Site Settings page, select Query Rules from the Search section.
    image7
  2. In the Select a Result Source…dropdown box, select Local SharePoint Results (system)
  3. Click the New Query Rule link at the top of the page and fill in the following fields:image8
    1. Rule name: Story
    2. Click Remove Condition (this allows the rule to fire on any query text)
    3. Click Add Result Block:
      1. Block Title: Stories for “{subjectTerms}”
      2. Query: click Launch Query Builder
        1. Property filter: PageTypeOWSCHCS Equals Story
        2. Click OK
      3. Click Settings
        1. Co not show a “more” link
        2. This block is always shown above core results
        3. Item Display Template: Story
    4. Click OK, and then Save.

Now, when we type a search term into the search box, we see all Story pages pushed to the top and displayed according to the HTML and CSS we specified in our display template.

image1

This is just one example of the many ways to leverage the power of search, and the advancements made in 2013, to drive users to specific content in your site while improving the overall user experience. When it comes to SharePoint and search, it just keeps getting better.

This tip contributed by Nicole Prestby, an Abel Solutions SharePoint Consultant.