AJAX Quickstarts   |   Silverlight Quickstarts   |   Dynamic Data Controls Quickstarts   |   Services Quickstarts   |   Dynamic Languages Quickstarts   |   ASP.NET AJAX Home   |      |  I want my samples in...     

ASP.NET Futures (July 2007): Enabling Search for your Web Site with ASP.NET AJAX

This section provides information about the following:

  • Using a search provider to add search capabilities to your site. A search provider takes a query and returns a set of search results.

  • Adding the SearchDataSource control to a Web page. The SearchDataSource control uses search providers to obtain search results that you can bind to by using a control such as the Repeater control.

  • Writing a custom search provider. You can write a custom search provider if you want to use the capabilities of a specific search engine.

Adding Search to Your Web Site

You can use features introduced in the ASP.NET Futures (May 2007) release to easily add search capabilities to your ASP.NET Web sites. Rather than implement a search engine yourself (and potentially have to index your site, etc.) you can take advantage of the capabilities that commercial search sites expose by using their APIs. You can gather search strings, send them to the asscoiated search APIs (along with a filter for your site), and then display the search results.

To enable this capability, the ASP.NET Futures (May 2007) release introduced a search provider architecture that exposes an API for calling search services. The provider model makes it possible to call different search services using a consistent API. The Futures release includes a provider for Windows Live Search. You can create your own providers for working with other search services.

Note  To call the APIs of commercial search services, you typically need to register with the search service and obtain a key that you pass as part of a search call. It is up to you to get the key for the search services that you want to use.

To further simplify the process of calling a search service, the Futures release includes a SearchDataSource control. You can bind the SearchDataSource to a text box where users can enter search strings. The SearchDataSource calls the underlying provider and passes it the search string. The control also exposes the search results in a format that you can bind a data control to.

Adding a Search Provider

The first step to adding search to your Web site is to configure a search provider in the search section of the application's Web.config file.

Windows Live Search Provider

The ASP.NET Futures (May 2007) release introduced a search provider for Windows Live Search. The provider makes Web services call to the Windows Live Search service on the Internet to obtain search results for a specified site domain (for example, ajax.asp.net).

Note  Before you use the Windows Live Search provider, verify that Windows Live Search is already indexing (crawling) your Web site. In the Windows Live Search page (http://search.live.com), run a search and include the site: keyword using syntax like site:yoursite.com. If Windows Live Search is not indexing your Web site, you can subsmit your site's URL to Windows Live Search. For more information, see http://search.live.com/docs/.

Each application requires a unique application ID from Windows Live Search. The appID is specified in the provider registration in the Web.config file. Follow these steps to create an application ID for the Windows Live Search service:

  1. In the browser, go to http://search.msn.com/developer.
  2. Click Create and Manage Application IDs.
  3. Sign in with your Windows Live ID.
  4. Click Get a new AppID.
  5. In the Application Name text box, enter a friendly name for your application.
  6. Copy the application ID and paste it into your Web.config file as the value of the appID property, as shown in the following example.

		    
<microsoft.web.preview>
...
<search enabled="true">
    <providers>
        <add name="WindowsLiveSearchProvider" 
        type="Microsoft.Web.Preview.Search.WindowsLiveSearchProvider, Microsoft.Web.Preview"
        appID="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 
        siteDomainName="xxxxxx.xxxx.xxx" />
    </providers>
</search>
...
</microsoft.web.preview>    
            
web.config

Adding the SearchDataSource Control to a Web Page

To simplify using the search provider, you can add a SearchDataSource control to your ASP.NET Web pages. The SearchDataSource control uses the search providers configured in the Web.config file to obtain search results, and it provides a data binding source to data controls such as the GridView, DataList, or Repeater controls.

To get a search string, you can bind the query property of the SearchDataSource control to a text box, as shown in the following example.

		    
<form id="form1" runat="server">
<div>
    <asp:TextBox ID="TextBoxSearch" runat="server"></asp:TextBox>
    <asp:Button ID="ButtonSearch" runat="server" OnClick="ButtonSearch_Click" Text="Search" />
       
            
    <asp:SearchDataSource ID="SearchDataSource1" runat="server" >
        <SelectParameters>
            <asp:ControlParameter ControlID="TextBoxSearch" Name="query" PropertyName="Text"
                Type="Object" />
        </SelectParameters>
    </asp:SearchDataSource>
     
    
    <asp:Repeater ID="RepeaterSearchResults" runat="server" DataSourceID="SearchDataSource1">
        <ItemTemplate>
            <h2>
            <asp:HyperLink ID="SearchResultLink" runat="server" 
                        NavigateUrl='<%# Eval("Url") %>' 
                        Text='<%# Eval("Title") %>'>
            </asp:HyperLink>
            </h2>
            <asp:Label ID="SearchResultDescription" runat="server"
                        Text='<%# Eval("Description") %>'>
            </asp:Label>
            <asp:Label ID="Label1" runat="server"
                        Text='<%# Eval("Url") %>'>
            </asp:Label>
            <p></p>
        </ItemTemplate>
    </asp:Repeater>
</div>
</form>    
            
ASP.NET Web page

		    
protected void ButtonSearch_Click(object sender, EventArgs e)
{
    SearchDataSource1.DataBind();
}
            
C#

Creating a Custom Search Provider

To create a custom search provider, create a class that derives from SearchProviderBase and implement the Initialize and Search methods. The Initialize method processes the entries in the Web.config file for the provider. The Search method executes the search and returns a collection of type SearchResult.

		    
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Configuration.Provider;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;

using Microsoft.Web.Preview.Search;

namespace Microsoft.Web.Samples.Search
{
    public class YahooSearchProvider : SearchProviderBase
    {
        //...

        public YahooSearchProvider() : base()
        {
        }

        public override SearchResult[] Search(SearchQuery searchQuery)
        {
            //...
        }

        public override void Initialize(string name, NameValueCollection config)
        {
            //...
        }

    }
}
            
code

Search Provider Example

The following example shows a complete example of search providers. The example includes two search providers, one for a commercial search service and the other for Microsoft Index Server.

Enabling Search
View Source


This topic is Microsoft ASP.NET Futures pre-release documentation and is unsupported by Microsoft. Blank topics are included as placeholders and existing content is subject to change in future releases.