Thursday, December 6, 2007

Displaying Google Search Results in the MOSS Search Center

I thought of a couple of possibilities to meet this requirement, but the only approach I could get to work satisfactorily in a short space of time was to embed a call to the Google AJAX Search API inside a content editor web part.

To achieve this, first you need a Google Search API Key which you can get from a sign up form on the Google Code site. The process creates a key that will work against a specific server URL; surprisingly, the correct key actually permits the search to work from an intranet site, not just from public-facing sites. The page displaying your key also includes some starter code to show how to use that key - nice work, Google!

I modified this code somewhat to restrict search results to New Zealand sites only by applying the following restriction to each Searcher object:

searcher.setSiteRestriction("co.nz");

Had hoped for a more generic way of restricting the search to a country, but that's all I have found so far.

Then the JavaScript code, including references to the search css file "gsearch.css" and to the API launcher, was pasted into a Content Editor control on the search results page in the search center. One addition was a querystring parsing method in JavaScript to read the search term from the "k" querystring parameter in the URL for the search results page, and to use that text in the SearchControl execute call.

Certainly not as flexible or simple a process as that explained by Arpan Shah in his blog post about rendering Microsoft Live search results in SharePoint, but a good starting point to fulfill the client's requirements.

5 comments:

Matt said...

Excellent post, I would like to add the Google control to my search results page and pull the k= query from the title bar... would you please elaborate on how to get the parameter from the title bar into the Google code?

Thanks in advance.

Matt

whats.to.learn.today said...

Hi Matt,

I used the following JavaScript method in the content editor to parse the querystring:

function getQueryString() {
var querystring = new Array;
// parse current url into an array with the keys/values
var q = String (document.location).split('?')[1];
if (!q) return false;
q = q.split ('&');

for (var i = 0 ; i < q.length; i++) {
// for each key/value, split them at the '='
// and add them to the qerystring array
var o = q[i].split('=');
querystring[o[0]] = o[1];
}

// return the querystring
return querystring;
}

Then get the search term as follows:

var qs = getQueryString();
var searchTerm = qs['k'];

Matt said...

Very good, that gets the k= query passed to the google search, but now I am running into difficulties when searching for a multi-word query for instance:
Searching for "Titan America" (my company) searches for "Titan%20America" in the Google control. Any ideas?

whats.to.learn.today said...

You can use

var searchTerm = unescape(qs['k']);

unescape() is a core JavaScript function that decodes a string.

systems.development said...

Hi, I'm new to MOSS development and really like the idea in your post but am having a little trouble implementing it :S

Any chance you could post the actual code from the Content Editor Web Parts you use? I'm hoping that would give me the jumpstart I need :)

Thanks,
Ivan