Click here to Skip to main content
13,352,631 members (34,375 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 12 Feb 2008

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.


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


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


                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()

                '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)

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

            End Try

            Dim litOut As New Literal
            litOut.Text = "Please enter a search term."
        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
        Dim m_TransformXml As New System.Web.UI.HtmlControls.HtmlGenericControl
        Dim sw As StringWriter = New StringWriter()
        Dim xslDoc As New XslCompiledTransform()

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

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

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

    End Function 

XSLT Transform File

<xsl:stylesheet version="1.0" xmlns:xsl="">
  <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">
              <xsl:value-of select="title" />
        <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="' ...'" />
        <br />
        <xsl:value-of select="link" />
        <br />
        <br />
      <xsl:if test = "position()=last()">
        <hr />
          <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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jason Witty
Software Developer (Senior)
United States United States
For more articles please visit my website:

A Programmers Journal

You may also be interested in...


Comments and Discussions

QuestionGoogle? Pin
thany.org13-Feb-08 22:57
memberthany.org13-Feb-08 22:57 
AnswerRe: Google? Pin
Niiiissssshhhhhuuuuu14-Feb-08 0:50
memberNiiiissssshhhhhuuuuu14-Feb-08 0:50 
GeneralRe: Google? Pin
Jason Witty14-Feb-08 9:56
memberJason Witty14-Feb-08 9:56 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 12 Feb 2008
Article Copyright 2008 by Jason Witty
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid