Adding Suggestions to the Sharepoint search box : Part 2

In Part 1 of this post we used a simple javascript to display suggestions on a custom search box. After I wrote that post I was thinking it would make more sense to read the suggestions from a list instead of a JavaScript file. This post will explain how to setup the list and edit the JavaScript to read the suggestions from a list. The idea is that an administrator could manage the list and help guide users toward specific information instead of getting numerous un-related results like in those commercials for bing.

Assuming you already have the search box setup as described in Part one of this post all you will need to do is change the javascript and add some code to the page.


  • Do everything from the first post
  • Add a link to jQuery somewhere on the page.
  • Create a custom list called ‘Suggestions’. It just needs to have the default title field. Create at least one item in the list and in the title enter several comma separated words.
  • Add the code below to the page.
<script type="text/javascript” src=“”></script>
<script type="text/javascript">
function GetItemsFromSP() {
    var soapEnv =
 “<soapenv:Envelope xmlns:soapenv=''> \
                <soapenv:Body> \
                     <GetListItems xmlns=''> \
                        <listName>Suggestions</listName> \
                        <viewFields> \
                            <ViewFields> \
                               <FieldRef Name='Title' /> \
                           </ViewFields> \
                        </viewFields> \
                    </GetListItems> \
                </soapenv:Body> \
        url: “_vti_bin/lists.asmx”,
        type: “POST”,
        dataType: “xml”,
        data: soapEnv,
        complete: processResult,
        contentType: “text/xml; charset=\”utf-8\”“,
        async: false
function processResult(xData, status) {
    $(xData.responseXML).find(“z\\:row”).each(function() {
        $(“#data”).append(“” + $(this).attr(“ows_Title”) + “,”);
<script type=“text/javascript” src=“Resources/searchfield.js”></script>
<div style=“display:none; visibility:hidden” id=“data”></div>

The code above get the items from the Suggestions list, seperates them with commas and puts them in a hidden div with an ID of data. Now we just need to edit the searchfield script to read in the information from our hidden div. To do this edit searchfield.js line 26 to call the GetItemsFromSP function and set the suggestionText variable to be equal to the contents of our hidden div. Here is the code.

	var suggestionText =  $(“#data”).html();

Test the page and make sure it works by typing the first few letters of one of the words in you list item.


This is very similar to Jan Tielens’ SharePoint Search-as-You-Type with jQuery solution but I wanted the search box to still be usable even if there are no suggestions. In Jan’s solution the ‘search box’ does not actually do anything when the magnifying glass is clicked. There are likely better ways to do this and if you know of one please post a comment.

Add a Comment

Your email address will not be published. Required fields are marked *