Enable Query Suggestions in SharePoint 2010

The concept of query suggestions is already well described in the Manage query suggestions TechNet article. One thing you might have noticed is that query suggestions are only enabled by default in the Search Center site templates (available for both SharePoint Search and FAST Search) but are not enabled for the search box located in the page header of other standard SharePoint site templates. In this blog post I’m going to show how to override the default behavior and enable search query suggestions for sites created using the Team Site template without modifying the out-of-the-box master page.

First, let’s go ahead and create some query suggestions using the following PowerShell command:

$searchapp = Get-SPEnterpriseSearchServiceApplication -Identity "FASTQuery"

New-SPEnterpriseSearchLanguageResourcePhrase -SearchApplication $searchapp -Language En-Us -Type QuerySuggestionAlwaysSuggest -Name "M300 Digital Camera"
New-SPEnterpriseSearchLanguageResourcePhrase -SearchApplication $searchapp -Language En-Us -Type QuerySuggestionAlwaysSuggest -Name "M400 Digital Camera"
New-SPEnterpriseSearchLanguageResourcePhrase -SearchApplication $searchapp -Language En-Us -Type QuerySuggestionAlwaysSuggest -Name "M500 Digital Camera"
New-SPEnterpriseSearchLanguageResourcePhrase -SearchApplication $searchapp -Language En-Us -Type QuerySuggestionAlwaysSuggest -Name "X200 Digital Camera"
New-SPEnterpriseSearchLanguageResourcePhrase -SearchApplication $searchapp -Language En-Us -Type QuerySuggestionAlwaysSuggest -Name "X250 Digital Camera"
New-SPEnterpriseSearchLanguageResourcePhrase -SearchApplication $searchapp -Language En-Us -Type QuerySuggestionAlwaysSuggest -Name "Z500 Digital Camera"

Start-SPTimerJob -Identity "Prepare query suggestions"

Once the timer jobs completes, we should see query suggestions in the search center:

But what about the standard site pages? Well, the query suggestions are disabled there by default. Next, we are going to build a new site collection feature that turns the query suggestions on for the entire site collection.

Let’s go ahead and create a new empty SharePoint 2010 project in Visual Studio 2010 and add a new site collection feature.

Now we need to add a new Empty Element item to the project and populate the Elements.xml file with the following content:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
	<Control
		Id="SmallSearchInputBox"
		Sequence="24"
		ControlClass="Microsoft.SharePoint.Portal.WebControls.SearchBoxEx"
		ControlAssembly="Microsoft.Office.Server.Search, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
		<Property Name="GoImageUrl">/_layouts/images/gosearch15.png</Property>
		<Property Name="GoImageUrlRTL">/_layouts/images/gosearchrtl15.png</Property>
		<Property Name="GoImageActiveUrl">/_layouts/images/gosearchhover15.png</Property>
		<Property Name="GoImageActiveUrlRTL">/_layouts/images/gosearchrtlhover15.png</Property>
		<Property Name="UseSiteDefaults">true</Property>
		<Property Name="FrameType">None</Property>
		<Property Name="ShowAdvancedSearch">false</Property>
		<Property Name="DropDownModeEx">HideScopeDD_DefaultContextual</Property>
		<Property Name="UseSiteDropDownMode">true</Property>
		<Property Name="ShowQuerySuggestions">True</Property>
	</Control>
</Elements>

Your project structure at this point should be similar to the screenshot below:

Build and deploy the solution package and activate the site collection feature. Start typing the search query and you should see the query suggestions now but it’s obvious that the out-of-the-box SharePoint styles don’t work properly in this case:

In order to resolve this style issue we have to override a couple of the SharePoint styles. Start by right-clicking the project in Solution Explorer, then going to Add -> SharePoint “Layouts” Mapped Folder. Then add a new css file with the following content:

.s4-search INPUT {
	FLOAT: none !important
}
.s4-rp DIV {
	DISPLAY: block !important
}

The last step is to add the new css file reference to the SharePoint master page. We’ll do it by updating the existing Elements.xml to add a custom CssRegistration to AdditionalPageHead. Here’s the final Elements.xml file content and project structure:

Deploy the solution package once again and check out the query suggestions styling now – everything should look and work as expected now:

References:

  1. Manage query suggestions (SharePoint Server 2010)
  • I followed your instructions on the “Enable Query Suggestions in SharePoint 2010” exactly (BTW, they are really clearly written and easy to understand. Thanks.) Everything has worked, but I don’t see the suggestions on the search box like I should. Is there any update on this?

  • Vassili Altynikov

    Do you see any query suggestions on the Results.aspx page in the Search Center site? The query suggestions are enabled there out-of-the-box so that would confirm if the suggestions were seeded successfully or not.

  • Yes. I do see the suggestions on the results.aspx page. The one thing I was unsure about (I’m a complete Visual Basic novice) was — Should I have deployed this as a sandboxed solution or a farm solution?

  • Vassili Altynikov

    I should’ve made it more clear in the instructions – the assumption is that you deploy this as a farm solution to a Team Site that uses an out-of-the-box site template and master page (the master page has to have a DelegateControl placeholder with the ID of SmallSearchInputBox in order for this to work).

  • Harish

    How can I get the search suggestions from the search engine automatically without having to add all suggestions manually ( as added with powershell command, New-SPEnterpriseSearchLanguageResourcePhrase). I would like the default ( the search center suggestions) to be shown up on this DelegateControl placeholder with the ID of SmallSearchInputBox.

    • Vassili Altynikov

      @Harish, you should see the same query suggestions in this search box as on the search center site. I only used the PowerShell script to add some sample query suggestions in my test environment and you can ignore that step if you already have query suggestions available in your environment. According to the Manage query suggestions (SharePoint Server 2010) TechNet article,

      Query suggestions depend on users’ searches. Only search queries that have been previously returned and then clicked through at least six times will appear in either the search box list or the Related Queries Web Part.

      • Harish Vajja

        Thanks Vassili Altynikov. Worked great. I can keep the option of adding new terms as users requests the words that are not part of auto list :-).