Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A completely customizable Google API Web Client

0.00/5 (No votes)
15 Jul 2003 1  
A web based Google Web services client using ASP.NET.

Sample Image - mygoog.jpg

Introduction

This is a demonstration of a web based client for Google Web Services. This is my first attempt at ASP.NET technology.

I have tried to make it fully customizable and open ended as far as possible.

Key customization features are:

  1. The search result display is controlled by an XSL file (search.xsl). Change the look and feel and have fun customizing the result set the way you want it.
  2. The application reads from a cfg.xml file on start up. It contains all the search query parameters and also some application specific settings. For example, the key required to access the Google search database. You can find this file in the attached zip fie.

The app looks for cfg.xml in the current working directory which would be c:\winnt\system32 by default. Copy this file to that location.

Advantage of having this is search query terms can be changed as and when requirements change, and same holds good for the results display.

This application allows a user to go through the first 1000 results since Google places that restriction.

What do you need to run the demo project?

  1. IIS 5.0 on Windows 2000/.NET Framework version 1.0.
  2. Required: Register with Google and get your personal license key. This is a must.
  3. Required: Open the cfg.xml included in the source and fill in the values for <key/>, <ResultsPath/> and <XslPath/>. Explanation of these keys in a moment...

What the Application does

Step 1:

The application sends a SOAP message to the Google Server which in turn returns a Google proprietary data structure. The application then parses this data structure and creates an XML file. (Let's call it results.xml. Note: It could be any name you choose as long as it's in the cfg.xml.) Here is a small portion of it:

<SearchResults attributes1=value1... >
<ResultSet>
<Item>
      <summary /> 
      <url>http://www.iqsoft.com/</url> 
      <snippet>PRODUCTIONS </snippet> 
      <title>Welcome to SAWStudio.com!</title> 
      <cachedsize>9k</cachedsize> 
      <relatedInformationPresent>True</relatedInformationPresent> 
      <hostName /> 
      <directorytitle /> 
</Item>
</ResultSet>
</SearchResults>
<SearchResults>

Step 2:

The application then transforms this XML file using the custom stylesheet (search.xsl) and writes the results of the transformation to the client browser.

OK, so that's what the <ResultsPath/> and <XslPath/> keys mean.

  • <ResultsPath/> is the fully qualified path to the file where you want the app to dump the XML.
  • <XslPath /> is the fully qualified path to the file where the stylesheet (search.xsl) is stored.

Key areas of the source code:

Search class in WebForm1.aspx.cs

The Search class defined in the code-behind of WebForm1.aspx, which is the only webform used in the entire project, has the core function dumpResultsasXml() which does exactly as named. System.XmlTextWriter does the writing.

The Transform() function does the transformation to HTML. I have used a MemoryStream object to read the transformation results, and subsequently, a StreamReader to read that memory and convert it to string data type.

The WebForm1.getdata is where the action actually happens. This can be called from within the WebForm1.aspx like this:

<% Response.Write(getdata()) %>

Points of interest:

There is only a single instance of the Google Search Service object which is initialized in Application_Start().

Although the "Next" and "Previous" links could have been generated by the search.xsl file, there is no way for us to control the starting index value in the XSL other than incrementing it by x like this:

<xsl:variable name="startindex" select="@currentIndex+10"/> ... and so on.

But the XSL would be blissfully unaware of what page we are on. The following code shows how it's been implemented:

if( !Page.IsPostBack) 
{ 
  start = Request.Params["start"]; 
  results = getdata(query,Convert.ToInt32(start));
}
else 
  results = getdata(query,0); //In this case start would always be 0.

Tracking is done using the querystring parameter start to which x is added or subtracted, to generate the URL for the "Next " and "Previous" links. In this case, x =10. It's a good idea to have this as one of the settings in the cfg.xml, so it would be more generic. Well, next release maybe :)

Here is an example of how the Next link is generated, where start is the value of the querystring parameter start. nTotalCount is the total number of results.

if((nTotalCount - nNext) > 10)
 {
              
     Response.Write("<br></br><a href=\"webform1.aspx?type=Next&start=");
     Response.Write(nNext);
     Response.Write("&query=");
     Response.Write(TextBox1.Text);
     Response.Write("\"><b>Next</b></a><br></br>");  
}

Well, I hope I have covered the key areas. Like any other program, I am certain there would be bugs and mistakes I may have made. Looking forward to your feedback so I may incorporate enhancements and bug fixes if any.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here