Description
In this article, you'll learn how to use federated search in Windows 7 to easily locate items from various public and private sources directly from Windows Explorer.
Introduction
In this article, I've created a class file to encapsulate a federated search provider. This lets you use the new federated search feature of Windows 7. Here, you'll learn about:
- Federated search
- Serializing a class as an XML object
- Creating a simple Silverlight project to expose it
What is Federated Search?
Windows 7 now lets you search web sites from Windows Explorer. This isn't a hugely-publicized feature, and it's not really accessible to developers (as far as I know), but it's useful for anyone.
Windows 7 federated search is built on the OpenSearch specification developed by Amazon A9. The search itself takes place by making a simple HTTP GET request for a given search term specified in a URL (http://example?search=TEST). The response is the web site's results, formatted as RSS or ATOM. Any site that offers search can choose to also make the results available in one of these supported formats. Applications like SharePoint, Exchange Server and even Sticky Notes included with Vista and Windows 7 offer this ability.
OpenSearch reduces the bandwidth needed for a typical search response and allows developers to create interesting applications with the results. Of course, one of these applications is the ability to display results directly in Windows Explorer, including the ability to sort by columns, copy and paste, and use the common search field. Some custom providers even support full integration with the Windows shell, such as drag-and-drop for copying entities or viewing additional properties (think Outlook, SharePoint, or images).
In order for Windows to add an OpenSearch provider, you need to provide a definition file in OSDX format. This format specifies human-readable descriptive information, along with necessary URL's and other parameters.
Generating OpenSearch Definition Files
The purpose of this project is to create OpenSearch definition files-that is, the OSDX files. These files are associated with Windows Explorer and add themselves to the Searches folder: C:\Users\{USER}\Searches.
This code does not actually perform any searches or integrate with Windows; it's not a proxy and it doesn't scrape web pages. In fact, it doesn't do anything except create the OSDX files to add to your system.
To create an OSDX file, you need to provide a URL to invoke and a name for the provider. A minimal file looks like this:
<?xml version="1.0" encoding="UTF-8"?> <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"> <ShortName>Bing</ShortName> <Description>Get Relevant Results & Decide Faster. Bing Gets The Job Done!</Description> <Url type="application/rss+xml" template="http://www.bing.com/search?q={searchTerms}&go=&form=QBLH&qs=n&format=rss"/> </OpenSearchDescription>
Once you find the right URL, use "{searchTerms}" to replace the term. There are also escape expressions for starting index ("{startIndex}"), result counts per page ("{count}"), and more. Note that Windows Explorer is very picky about using a provider file. If there is anything malformed in the XML (be sure to escape ampersands in URL's!) or if you are missing an Atom or RSS URL, then it will fail with no useful information:
To make this easier, I created a class, OpenSearchDefinition, with the right properties. The class and properties are marked up with XML attributes so it can be written out as well-formatted XML without any string concatenation.
It can be difficult to find sites that offer RSS search results, but fortunately there's another way. Bing searches the internet and can be scoped to a given site's content. Even better: you can request search results in RSS. Google provides RSS search results too, but you need to pre-configure your searches. Bing allows you to do this on-the-fly, as a parameter for any search.
Creating the Front-End
The OpenSearchDefinition class file does all of the work, and could be reused, but doesn't have a broad usage story. I created a Silverlight front-end to demonstrate one way of using it. An ASP.NET Web Form sits on the back-end and serves up the OSDX file based on URL parameters. The Silverlight application just provides a convenient way to enter those parameters.
The first tab is a way to create a provider for any site by using Bing. Remember that if you scope the search using the site parameter, Bing will return search results for only one site - effectively a site-level search.
The second tab is for building up an OSDX file using any site's RSS search URL. You can enter a custom name, description, and search URL. This is the best method for adding new search providers.
Though Bing can be used to search most sites, you won't be able to take advantage of all features a site has to offer.
Conclusion
Currently, very few site owners seem interested in offering syndicated search results-this may be because they're concerned about ads not being seen. But of course, if I perform a search and view the results, I'll likely end up on the site and see the ads anyway! So if you run a popular site and have a search feature (don't they all?) consider offering that little RSS/ATOM link next to results.
From the consumption point of view, it's really easy to create a search provider definition using the OSDX format. You could do it by hand, but this tool simplifies the process and shows off Silverlight, XML serialization, and a few other nifty features as a bonus!
The OpenSearchDefinition class file is ready to reuse. As with all Coding 4 Fun code, you can do whatever you want with it. Let me know if you find other uses for it!
Download the free Visual C# 2008 Express Edition so you can play with the code, or download the ASP.NET web site and give it a try.
Additional Information
- http://www.neowin.net/news/main/09/01/06/windows-7-federated-search
- http://www.msdev.com/Directory/Description.aspx?EventId=1199
- http://www.sevenforums.com/tutorials/742-windows-7-search-federation-providers.html
Arian Kulp is a software developer living in Western Oregon. He creates samples, screencasts, demos, labs, and articles, he speaks at programming events, and he enjoys spending time with his family. Check out his homepage to contact him or learn more.