Click here to Skip to main content
Click here to Skip to main content

Search Your Site Using Windows Live Search (Part II)

, 12 Feb 2008
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jason Witty
Software Developer (Senior)
United States United States
For more articles please visit my website:
http://www.aprogrammersjournal.com
 
A Programmers Journal
RSS

Comments and Discussions

 
QuestionGoogle? Pinmemberthany.org13-Feb-08 21:57 
AnswerRe: Google? PinmemberNiiiissssshhhhhuuuuu13-Feb-08 23:50 
GeneralRe: Google? PinmemberJason Witty14-Feb-08 8:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 12 Feb 2008
Article Copyright 2008 by Jason Witty
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid