Announcing: OpenSearch on ASP.NET made super easy with the OpenSearch Toolkit

OpenSearch is a technology that already has widespread support across the web and is now getting even more relevant with Internet Explorer 8’s Visual Search feature and the Federated Search feature in the upcoming Windows 7 release.

Recently I blogged about some work I’d been doing with OpenSearch and how frustrating the whole process was. By the time you build feeds for IE8, Firefox and Windows 7 you’ve touched on “standards” documented by Amazon, Yahoo, Mozilla and Microsoft. Good luck tracking down all that info and working out the discrepancies!

As a first step to making this easier, I released the OpenSearch Validator so we had a quick way to track down all the potential issues and get a clear indication of whether our OpenSearch implementation was going to work in all environments we wanted it to. I also released the source code for this on CodePlex so that you can run it in your internal dev environments or even integrate it into your build process.

Now it’s time to make it even easier. Ducas Francis, one of the other members of my team, took on the job of building out our JSON feed for Firefox as well as our RSS feed for Windows 7 Federated Search. More formats, more fiddly serialization code. Following this, he started the OpenSearch Toolkit; an open source, drop-in toolkit for ASP.NET developers to use when they want to offer OpenSearch.

Today marks our first release.

Implementing OpenSearch

First up, download the latest release of the project from CodePlex and add it to your project references:

Reference

Next, add a new Generic Handler to your project called OpenSearch.ashx:

GenericHandler

In the code behind for the handler (OpenSearch.ashx.cs), remove the autogenerated code and change the base class to OpenSearchHandler:

BaseClass

Now, just start implementing the abstract properties and methods on the base class.

The first one you’ll want to implement is the Description property. This returns the basic meta data about your provider that will be shown when users choose to add it to their browser’s list of search providers. You also need to specify the SearchPathTemplate which is a format string that the OpenSearch Toolkit will use to generate links to your site’s search page.

Description

Next, implement each of the data methods. GetResults is the most important one that you need to implement. It should return an array or collection of about 5 to 8 search results, preferably with thumbnail images.

Implementing GetSuggestions is a little bit harder, and we don’t expect everyone to do it. The idea of this method is to return suggestions for other search terms. For example, if the supplied term was “Aus” you might return “Australia” and “Austria” as suggestions. The process of generating these results from your data is naturally a bit harder.

Results

At this point you have a fully functional OpenSearch endpoint.

The last step is to tell the world about it by adding a small snippet of HTML to the <head> section of each of your pages:

<link title="My Site" rel="search" type="application/opensearchdescription+xml" href="~/OpenSearch.ashx" />

Voila! Your users will now get to experience full OpenSearch support from your website:

VisualSearch

5 comments

  1. hi Tatham
    how can i enable the suggestions?
    i made modification , is it ok?
    Results = SearchHelper.PerformSearch(SearchTerm);
    if (Results.Count() == 0)
    {
    Suggestions = SearchHelper.GenerateSuggestions(SearchTerm);
    }

    1. Hi Alaa,

      You should only need to do two things to support suggestions. Both changes are in your own handler.

      1) return true from SupportsSuggestions
      2) implement GetSuggestions

      Also note that suggestions are only for Firefox. IE8 uses the results feed.

      Thanks,

      Tatham

  2. Hi Tatham –

    I fired up this project in VS2010 on IIS7 with the IIS6 Configuration migration option enabled and using your OpenSearchValidator project the service apparently isn’t returning proper responses. Have you given it a try on IIS7 or VS 2010? Any suggestions would be appreciated.

    Here is my GetResults method, I single stepped it so I know it is being invoked:

    protected override IEnumerable GetResults(string searchTerm)
    {

    List results = new List();
    results.Add(new SearchResult() { Description = “a”, GroupTitle = “b”, ImageUri = new Uri(“http://farm4.static.flickr.com/3068/2587076826_17fd16a7b8.jpg”), Path = “”, Title = “M” });

    return results;
    }

  3. Hi

    I tried the sample and everything compiled. However when I choose the search provider and enter a keyword and press the search, it tries to go to the Search.aspx Page??? What do I need to implement there?

Comments are closed.