SharePoint 2010 autocomplete textbox containing listitems

Request:

A simple textbox where users type in a certain value. based on the this value,a suggestion appears by searching the values of a specific field in a specified list.
The solution has to be easy to maintain, performance is very important (more than 100 000 items) and it has to support all the browsers that SharePoint 2010 supports.
finally, it has to be a sandbox solution.

Solution:

Used technologies:

  • out-of-the-box SharePoint 2010 REST Services
  • JQuery & JQuery UI

Code:


<script language="ecmascript" type="text/ecmascript">

// Settings
var url = "http://vwds242/_vti_bin/listdata.svc/Large()";
var field = "Title";

// Onload
$(document).ready(function () {
	$("#tags").autocomplete({
		source: function (req, add) {
			var suggestions = search(req.term, url, field);
			add(suggestions);
		}
	});
});

// Search all the listitems by using the REST Service
// Value is the text that needs to be used in the query
// listurl is the listdata.svc url withouth the filter params
// field is the name of the field where the value in exists
function search(value, listurl, field) {
	var coll = new Array();
	var url =
		listurl + "?$filter=startswith(" + field + ",'" + value + "')";

	$.ajax({
		cache: true,
		type: "GET",
		async: false,
		dataType: "json",
		url: url,
		success: function (data) {
			var results = data.d.results;
			for (att in results) {
				var object = results[att];
				for (attt in object) {
					if (attt == field) {
						coll.push(object[attt]);
					}
				}
			}
		}
	});
	return coll
}
</script>
	
VN:F [1.9.20_1166]
Rating: 7.8/10 (15 votes cast)
VN:F [1.9.20_1166]
Rating: +2 (from 4 votes)
SharePoint 2010 autocomplete textbox containing listitems, 7.8 out of 10 based on 15 ratings

8 thoughts on “SharePoint 2010 autocomplete textbox containing listitems

  1. Thanks for this, though I could not get it to work :-(

    Questions:
    1) Does this need reference to any JQuery files? Which ones please?
    2) In settings the “Large()” is the “Large” the list name? Or what is it?

    Thanks a lot buddy, I hope to make it work for me.

    Cheers.

    VA:F [1.9.20_1166]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.20_1166]
    Rating: 0 (from 0 votes)
  2. This worked, thanks!!! To the above, Large() is the name of the SP List, I included the latest jquery and jquery UI plugin

    VA:F [1.9.20_1166]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.20_1166]
    Rating: 0 (from 0 votes)
  3. Sorry I am new to JQuery, and will greatly appreciate if you could give the specific file names for what you say is “latest jquery and jquery UI plugin”.

    Thanks a lot :-)

    VA:F [1.9.20_1166]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.20_1166]
    Rating: 0 (from 0 votes)
  4. Pingback: SharePoint @ Big Scholar » Blog Archive » SharePoint 2010 autocomplete textbox containing listitems

      • Hi Thanks a lot TOM,

        I was not aware of how to use REST, now I could able to run this in my sites home page, and I referenced both J query and UI plugins in the web part itself, however when I added this web part to another page, it doesn’t seem to be working, I am getting an error like, $(“#tags”).autocomplete is not a function. My input ID is tags itself. Can you kindly give some suggestions on this?

        VA:F [1.9.20_1166]
        Rating: 0.0/5 (0 votes cast)
        VA:F [1.9.20_1166]
        Rating: 0 (from 0 votes)
  5. Hi… thanks.. it works great.

    just a question..

    Why Sandbox solution?

    VA:F [1.9.20_1166]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.20_1166]
    Rating: 0 (from 0 votes)