Add google search to .Net website


If you need to add google search results to an existing asp.Net website there are a few options. This link specifies the differences in the the two products, Google Custom Search (GCS) VS. Google Site Search (GSS) :

It will cost $100/year to customise this but the standard edition with adverts is free. The Bing equivalent is also Free:

If you use the GSS then the code

if (!String.IsNullOrEmpty(Request.QueryString.Get("q")))
 _searchTerm = Request.QueryString.Get("q");

 int currentPage = Convert.ToInt32(SiteUtils.RequestValue("p", "1"));
  int pageStart = (currentPage - 1) * 10;

 string searchUrl = "" + ConfigurationManager.AppSettings["GSSearchIndexID"] + "&client=google-csbe&output=xml_no_dtd&q=" + _searchTerm;

  XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.XmlResolver = null;

  XslCompiledTransform xslTran = new XslCompiledTransform();

  xslTran.Load(AppDomain.CurrentDomain.BaseDirectory + "\\Common\\GoogleTransform.xslt");

  StringWriter writerString = new StringWriter();
  XsltArgumentList args = new XsltArgumentList();

 string pagingUrl = HttpContext.Current.Request.Url.LocalPath;

  pagingUrl = String.Concat("?q=", Server.UrlEncode(_searchTerm), "&p=");

 args.AddParam("page", "", currentPage);
 args.AddParam("paging-url", "", pagingUrl);
 args.AddExtensionObject("hh:SiteUtils", new SiteUtils());

 xslTran.Transform(xmlDoc, args, writerString);
 string output = writerString.ToString();
 _resultsHtml = output;

Useful links:

GoogleTransform XSLT file attached

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>