Creating a Vanity Search Page
- Posted: Mar 10, 2007 at 12:09 PM
- 696 Views
- 8 Comments
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
| It's all about you. Learn how to call the Windows Live Search service to create a vanity page that displays images, news articles, and websites about you. | |
|
Difficulty: Easy
Time Required:
Less than 1 hour
Cost: Free
|
|
This month, we create a vanity search page. The vanity search page displays any images, news or website entries about a particular person (for example, you). To create this page, we take advantage of the Windows Live Search API. This free API enables you to retrieve search results by calling a web service that Microsoft exposes on the web.
I decided to write this article because I wanted an excuse to play with the Windows Live SDK (see http://dev.live.com). The SDK includes documentation and samples for accessing all of the Windows Live web services including the Windows Live Search Web Service, Virtual Earth, and Windows Live Alerts.
I fully admit that I was actively avoiding writing this article because I dreaded the thought of needing to learn yet another Application Programming Interface. I was pleasantly surprised to discover that the API for Windows Live Search is both logical and painlessly simple to understand. I wrote the code for this article in less than an hour.
Before you can access the Windows Live services, you need to get an Application ID. You pass this ID to the Windows Live services to identify yourself. You get an Application ID by signing an agreement at the following URL: http://search.msn.com/developer/appids.aspx.
Figure 1 - Adding a Web Reference for Windows Live Search
After you have an Application ID, you are ready to start calling Windows Live web services. Here are the steps for creating the vanity search page:
1. Launch Visual Web Developer and create a new Web Site
2. Select the menu option Website, Add Web Reference to add a Web Reference to the Windows Live Search web service (see Figure 1). Enter the following address into the URL field: http://soap.search.msn.com/webservices.asmx?wsdl. When you click OK, a new Web reference named com.msn.search.soap is created.
3. Create a new Web Form Page by selecting the menu option Website, Add New Item. When you create the new Web Form Page, give it the name Vanity.aspx. Also, make sure that the checkbox labeled Place code in separate file is not checked.
4. Replace the contents of the page with the following code:
VB.NET Code
<%@ Page Language="VB" %> <%@ Import Namespace="com.msn.search.soap" %> <%@ Import Namespace="System.Text.RegularExpressions" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Const appId As String = "" Const query As String = """Bill Gates""" Const startMark As String = "\uE000" Const endMark As String = "\uE001" Private Sub Page_Load() ' Create instance of MSN Search Services Dim search As New MSNSearchService() ' Build Source Request Dim NewsSource As New SourceRequest() NewsSource.Source = SourceType.News Dim webSource As New SourceRequest() webSource.Source = SourceType.Web Dim imageSource As New SourceRequest() imageSource.Source = SourceType.Image imageSource.ResultFields = ResultFieldMask.Image Or ResultFieldMask.Url ' Build the Search Request Dim request As New SearchRequest() request.AppID = appId request.Requests = New SourceRequest() {imageSource, NewsSource, webSource} request.Query = query request.CultureInfo = "en-US" request.Flags = SearchFlags.MarkQueryWords ' Execute Search Dim response As SearchResponse = search.Search(request) lstResponses.DataSource = response.Responses lstResponses.DataBind() End Sub Private Function hiLite(ByVal item As Object) As String If item Is Nothing Then Return String.Empty End If Dim text As String = item.ToString() text = Regex.Replace(text, startMark, "<span class='hiLite'>") text = Regex.Replace(text, endMark, "</span>") Return text End Function Private Function toImage(ByVal objImage As Object) As String If Not objImage Is Nothing Then Dim image As com.msn.search.soap.Image = CType(objImage, com.msn.search.soap.Image) Return image.ImageURL End If Return String.Empty End Function </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> <style type="text/css"> html { font:12px Verdana; } a {text-decoration:none} .hiLite {background-color:yellow} </style> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList id="lstResponses" Runat="server"> <ItemTemplate> <h1><%# Eval("Source") %> (<%# Eval("Total") %> Results)</h1> <asp:Repeater id="grdResults" DataSource='<%# Eval("Results") %>' Runat="server"> <HeaderTemplate> <ul> </HeaderTemplate> <ItemTemplate> <li> <asp:HyperLink id="lnkResult" ImageUrl='<%# toImage( Eval("Image")) %>' Text='<%# hiLite( Eval("Title") ) %>' NavigateUrl='<%# Eval("Url") %>' Runat="server" /> <p> <%# hiLite( Eval("Description") ) %> </p> </li> </ItemTemplate> <FooterTemplate> </ul> </FooterTemplate> </asp:Repeater> </ItemTemplate> </asp:DataList> </div> </form> </body> </html>
C# Code
<%@ Page Language="C#" %> <%@ Import Namespace="com.msn.search.soap" %> <%@ Import Namespace="System.Text.RegularExpressions" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> const string appId = ""; const string query = "\"Bill Gates\""; const string startMark = "\uE000"; const string endMark = "\uE001"; void Page_Load() { // Create instance of MSN Search Services MSNSearchService search = new MSNSearchService(); // Build Source Request SourceRequest newsSource = new SourceRequest(); newsSource.Source = SourceType.News; SourceRequest webSource = new SourceRequest(); webSource.Source = SourceType.Web; SourceRequest imageSource = new SourceRequest(); imageSource.Source = SourceType.Image; imageSource.ResultFields = ResultFieldMask.Image | ResultFieldMask.Url; // Build the Search Request SearchRequest request = new SearchRequest(); request.AppID = appId; request.Requests = new SourceRequest[] {imageSource, newsSource, webSource}; request.Query = query; request.CultureInfo = "en-US"; request.Flags = SearchFlags.MarkQueryWords; // Execute Search SearchResponse response = search.Search(request); lstResponses.DataSource = response.Responses; lstResponses.DataBind(); } string hiLite(object item) { if (item == null) return String.Empty; string text = item.ToString(); text = Regex.Replace(text, startMark, "<span class='hiLite'>"); text = Regex.Replace(text, endMark, "</span>"); return text; } string toImage(object objImage) { if (objImage != null) { com.msn.search.soap.Image image = (com.msn.search.soap.Image)objImage; return image.ImageURL; } return String.Empty; } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Vanity Search</title> <style type="text/css"> html { font:12px Verdana; } a {text-decoration:none} .hiLite {background-color:yellow} </style> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList id="lstResponses" Runat="server"> <ItemTemplate> <h1><%# Eval("Source") %> (<%# Eval("Total") %> Results)</h1> <asp:Repeater id="grdResults" DataSource='<%# Eval("Results") %>' Runat="server"> <HeaderTemplate> <ul> </HeaderTemplate> <ItemTemplate> <li> <asp:HyperLink id="lnkResult" ImageUrl='<%# toImage( Eval("Image")) %>' Text='<%# hiLite( Eval("Title") ) %>' NavigateUrl='<%# Eval("Url") %>' Runat="server" /> <p> <%# hiLite( Eval("Description") ) %> </p> </li> </ItemTemplate> <FooterTemplate> </ul> </FooterTemplate> </asp:Repeater> </ItemTemplate> </asp:DataList> </div> </form> </body> </html>
Before you use the vanity search page, you should modify two of the constants that appear at the top of the page: the appId and query constants. Assign the App ID that you get from visiting the following URL to the appId constant: http://search.msn.com/developer/appids.aspx. Enter anything you want for the query constant. In the page above, the name Bill Gates (enclosed in quotes) is assigned to the query constant. When you open the page in a web browser, you should see a page similar to Figure 2.
Figure 2 - Image results for Bill Gates vanity search page
The page displays three different groups of results. First, the page displays a list of images that match the search query. Next, the page displays a list of news articles. Finally, the page displays a list of websites (see Figure 3).
Figure 3 - Web results for Bill Gates vanity search page
The Windows Live Search web service currently supports the following types of searches:
You perform a search by creating an instance of the SearchRequest class. You assign the text of your search query to the SearchRequest class's Query property. This class also includes a Requests collection. You add one or more SourceRequest objects to this collection that represent different types of searches (Image, Web, News, and so on). After you get all of the objects ready, you call the MSNSearchService.Search() method to actually perform the search.
The search results are displayed with the help of a DataList and Repeater control. The DataList displays the source of the search and the number of results for each source returned. The Repeater control is nested in the DataList and it displays the actual search results.
As I mentioned before, I wrote this article as an excuse to play with the Windows Live SDK. I am very happy that I got the chance to experiment with this SDK since I discovered that it is very easy to work with. I'm looking forward to trying out some of the other Windows Live services such as the Virtual Earth and Alerts services over the coming months.
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
very thanks for this Article
i use this article in my website
http://blogs.msdn.com/coding4fun/archive/2007/03/10/1855817.aspx Creating a Vanity Search Page
Can they be draged and droped on to a website page somehow.
PingBack from http://famousquotes.247blogging.info/?p=2482
You've been kicked (a good thing) - Trackback from DotNetKicks.com
Coding4fun examines how to use a web service in less than 1 hour by Creating a Vanity Search Page . Call
I recently visited an MIS department at Baylor University. Since my background is in engineering and
Hello.
I'm trying to use this with VS2008 Pro. I did get an application ID.
When i go to run it, i get this:
Client Error
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.Services.Protocols.SoapException: Client Error
Source Error:
Line 45: [return: System.Xml.Serialization.XmlElementAttribute("Response")]
Line 46: public SearchResponse Search(SearchRequest Request) {
Line 47: object[] results = this.Invoke("Search", new object[] {
Line 48: Request});
Line 49: return ((SearchResponse)(results[0]));
Any help would be gratefully appreciated.
Thanks,
Tony
Remove this comment
Remove this thread
close