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

Search Your Site Using Windows Live Search (Part II)

0.00/5 (No votes)
12 Feb 2008 1  
An Update to my original article which allows you to search your personal web site using Windows Live Search.

Introduction

This article is a follow up to my original article Search Your Web site with MSN Site Search. I noticed the article was still getting some traction so I decided to put together an updated version.

Changes in this version:

  • Code updated to Visual Studio 2008
  • Code refactoring for better encapsulation and performance
  • Enabled AJAX.NET support. Demo is now Ajax enabled
  • Updated deprecated xml.xsl transform calls
  • Fixed some pagination bugs
  • Adding a single retry on failure from feed
  • Updated name to Windows Live.
  • Improved general display output

Background

In the example above, I am building the query string on the fly with the search string typed in my Web form. I then parse the RSS feed with an XSL stylesheet and display the results. The pagination is accomplished by passing parameters to the XSL file and handling the pagination within the XSL. To view this example, click the link above.

Using the Code

As you can see, the search works very well and is very fast. You can try searching my site for something like "ASP" or "XML" to generate a sufficient amount of search results.

I am making my code available for download. Please feel free to download it and use it on your site. It will work on any server that will run ASP.NET. There are only two variables that you need to change to personalize the search results in your site. In the ASPX file, you will find two variables sitename and resultcount. sitename is the fully qualified domain name of your site and resultcount specifies the total number of results to return. You may want to adjust this based on the amount of content you have. The less total results you download on each query, the faster it will run.

.NET Code

 Sub DisplaySearchResults()

        'check if user entered a search term.
        If term.Text <> "" Then

            Try

                'init
                Dim msnXmlSrc As String = MSNSearchURL & sitename & "+_
                         " & searchterm & "&format=rss&FORM=ZZRE&count=_
                         " & resultcount & "&first=" & start
                Dim msnXslFile As String = Server.MapPath(XslTranformFile)

                'load output into xml document.
                Dim msnXmlDoc As XmlDocument = New XmlDocument()
                msnXmlDoc.Load(msnXmlSrc)

                'set parameters.
                Dim arglist As XsltArgumentList = New XsltArgumentList()
                arglist.AddParam("top", "", top)
                arglist.AddParam("page", "", pagenum)
                arglist.AddParam("searchterm", "", searchterm)

                'Transform xml/xsl
                Dim out As HtmlGenericControl = _
                    TransformXml(msnXmlDoc, msnXslFile, arglist)

                'add control to placeholder
                If ResultPLC.Controls.Count > 0 Then ResultPLC.Controls.RemoveAt(0)
                ResultPLC.Controls.Add(out)

            Catch ex As Exception
                Try
                    'retry once
                    If failcount > 0 Then Throw New Exception(ex.Message)
                    failcount = failcount + 1
                    DisplaySearchResults()
                Catch ex2 As Exception
                    'message error
                    Dim litOut As New Literal
                    litOut.Text = ex2.Message()
                    ResultPLC.Controls.Add(litOut)
                End Try

            End Try

        Else
            Dim litOut As New Literal
            litOut.Text = "Please enter a search term."
            ResultPLC.Controls.Add(litOut)
        End If

    End Sub 

Public Function TransformXml(ByVal xmldoc As XmlDocument, ByVal stylesheet As String, _
    ByVal params As XsltArgumentList) As System.Web.UI.HtmlControls.HtmlGenericControl
        'init
        Dim m_TransformXml As New System.Web.UI.HtmlControls.HtmlGenericControl
        Dim sw As StringWriter = New StringWriter()
        Dim xslDoc As New XslCompiledTransform()

        Try
            'perform transformation
            xslDoc.Load(stylesheet)
            xslDoc.Transform(xmldoc, params, sw)

        Catch ex As Exception
            'bubble exception
            Throw New Exception(ex.Message())
        Finally
            'free memory
            sw.Dispose()
            xslDoc = Nothing
        End Try

        'return control
        m_TransformXml.InnerHtml = sw.ToString()
        sw.Dispose()
        Return m_TransformXml

    End Function 

XSLT Transform File

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" />
  <xsl:param name="top" />
  <xsl:param name="page" />
  <xsl:param name="searchterm" />

  <xsl:template match="/">
    You searched for "<xsl:value-of select="$searchterm" />"<br />
    <hr />
    <xsl:for-each select="rss/channel/item">
      <xsl:if test="position() < ($top*($page+1)) + 1 and position() > $page * $top">
        <a href = "{link}">
          <span class="style1">
            <strong>
              <xsl:value-of select="title" />
            </strong>
          </span>
        </a>
        <br />
        <span class="style1">
          <xsl:value-of disable-output-escaping="no" 
                select="translate(substring(description,1,150),'/',' /')" />
          <xsl:value-of disable-output-escaping="no" select="' ...'" />
        </span>
        <br />
        <xsl:value-of select="link" />
        <br />
        <br />
      </xsl:if>
      <xsl:if test = "position()=last()">
        <hr />
        <div>
          <div class="paginate">
            <xsl:if test = "($page = 0) and ($top<last()-1)">
              <a href = "javascript:javascript:__doPostBack
                      ('LbHoldClick','{$page+1}');">next >

Points of Interest

Though it may be difficult for Microsoft to track where its feed is being used, the licensing information within the RSS file specifically states that the results are not to be used for commercial purposes. As I understand, Google API is free for all for up to 1,000 searches per day. So if you are building a commercial Web site, I would recommend using the Google search API, but if you are just looking to implement a great quick search on your personal Web site, then MSN site search is a great choice. If you do decide to use this script on your Web site, please post a link so I can take a look.

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