| 
Rank: Member
 Joined: 5/30/2008(UTC)Posts: 2
 
 | 
            
	      
                We developed this to link products between sites.  Place it in the BVModules folder under a directory and call the files xmlsearch.aspx and xmlsearch.aspx.cs It generates an RSS feed that can be rendered to HTML and placed as an ad based off the keywords on the page.  The link works as .../xmlsearch.aspx?keywords=word1+word2+word3 Modify it to fit your site.   
 Code:<%@ Page Language="VB" AutoEventWireup="false" CodeFile="XmlSearch.aspx.vb" Inherits="XmlSearch" ContentType="application/rss+xml" ResponseEncoding="UTF-8"%><%@ Import Namespace="BVSoftware.Bvc5.Core" %><?xml version="1.0" encoding="utf-8"?>
 
 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
 <channel>
 <title>__YOUR STORE__</title>
 <link>__YOUR URL__</link>
 <description>
 __YOUR DESCRIPTION__
 </description>
 <ttl>720</ttl><!-- 12 hours -->
 <language>en-us</language>
 <generator>P3:ASP-VB</generator>
 <pubDate><asp:Literal runat="server" id="datesstring"></asp:Literal></pubDate>
 <copyright><asp:Literal runat="server" id="yearstring"></asp:Literal> __YOUR COMPANY__</copyright>
 <atom:link href="http://__YOUR_URL__/BVModules/__LOCATION_DIRECTORY__/xmlsearch.aspx?keywords=special" rel="self" type="application/rss+xml" /> <!-- default to anything 'special' -->
 <textInput>http://_YOUR_URL_/search.aspx</textInput>
 <image>http://www.dhp.org/BVModules/Themes/__DIRECTORY_TO_YOUR_LOGO__/images/Banner.jpg</image>
 <asp:repeater ID="DataList1" runat="server" ><ItemTemplate><asp:Literal runat="server" id="itemparams"></asp:Literal></ItemTemplate>
 </asp:repeater>
 <asp:Literal runat="server" id="backupI"></asp:Literal>
 </channel>
 </rss>
 
 
 Code:Imports BVSoftware.Bvc5.Core
 Imports System.Collections.ObjectModel
 Imports System.Array
 Imports System.Collections
 Imports System.Web
 'include more so the auto complete features are available
 Imports System.Web.UI
 Imports System.Web.UI.WebControls
 Imports System.Web.UI.HtmlControls
 Imports System.Web.UI.Design
 
 
 
 Partial Class XmlSearch
 Inherits System.Web.UI.Page 'BaseSearchPage
 
 Protected randx As Random      'for random product selection
 Protected backupItem As String 'to guarantee a returned item if short
 
 
 Protected Sub Page_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 Dim kw As String
 
 'catch anything passed as keyword or keywords
 If Request.QueryString("keyword") IsNot Nothing Then
 kw = Request.QueryString("keyword")
 End If
 If Request.QueryString("keywords") IsNot Nothing Then
 kw = Request.QueryString("keywords")
 
 End If
 
 If kw IsNot Nothing Then
 
 randx = New Random(System.DateTime.Now.Millisecond)
 
 datesstring.Text = DateTime.Now.ToString("r")
 yearstring.Text = DateTime.Now.ToString("yyyy")
 
 ' A complete RSS item to default to if max items is not set
 backupItem = "<item><title>YOUR WEBSITE</title><description>Product Search</description><link>http://YOUR_URL/search.aspx</link><guid>YOUR_URL/#1</guid></item>"
 DoSearch(kw)
 Else
 ' Hard abort page, basic return, no RSS to cause error
 ' for custom programming I would recommend changing this to permit the
 ' RSS feed and return only the default/backup item
 Response.ClearContent()
 Response.StatusCode = 400
 Response.Write("<html><head><title>Entry Error</title></head>")
 Response.Write(vbCrLf)
 Response.Write("<body><h1>Keywords was empty</h1></body></html>")
 Response.Write(vbCrLf)
 Response.Flush()
 Response.Close()
 
 End If
 
 End Sub
 
 ' need a way to search categories for RSS and catch its BVIN
 ' cache the results of the BVIN to prevent re-running this function every call
 Private Function GetRssId() As String
 
 If System.Web.HttpRuntime.Cache.Get("RSSBVIN") IsNot Nothing Then
 Return DirectCast(System.Web.HttpRuntime.Cache.Get("RSSBVIN"), String)
 Else
 
 Dim cats As Collection(Of ListItem) = Catalog.Category.ListFullTreeWithIndents
 For Each li As ListItem In cats
 If li.Text.Equals("RSS") Then
 System.Web.HttpRuntime.Cache.Add("RSSBVIN", li.Value, Nothing, DateTime.Now.AddHours(12), TimeSpan.Zero, CacheItemPriority.NotRemovable, Nothing)
 Return li.Value
 End If
 Next
 End If
 
 ' fail and forget it if we cant find the RSS category for now
 System.Web.HttpRuntime.Cache.Add("RSSBVIN", "1", Nothing, DateTime.Now.AddHours(12), TimeSpan.Zero, CacheItemPriority.NotRemovable, Nothing)
 Return String.Empty
 End Function
 
 
 Private Sub DoSearch(ByVal keyword As String)
 ' define max (and min) results to return for the feed.
 Dim maxresults As Integer = 2
 ' This function, as is, currently uses 'x' to check that it does not return
 ' the same item (randomly) twice.  This works great when only returning 2 items
 ' if more are returned, x should be a collection and cross checked for each item grabbed
 
 Dim results As Collection(Of Catalog.Product)
 Dim totalRowCount As Integer = 0
 Dim criteria As Catalog.ProductStoreSearchCriteria = New Catalog.ProductStoreSearchCriteria
 criteria.Keyword = keyword  ' use the provided keywords
 
 ' if we can get the category ID for 'RSS' use it, otherwise search the site
 ' this way the results can be restricted to only what is permitted in the RSS feed
 Dim id As String = GetRssId()
 If Not id.Equals("1") Then
 criteria.CategoryId = id
 End If
 
 ' run the search
 results = Catalog.InternalProduct.StoreSearch(criteria, SessionManager.GetCurrentUserId, False, 0, 100, totalRowCount)
 
 ' declare the collection we are actually going to return
 Dim passed As Collection(Of Catalog.Product)
 ' create a check to make sure we don't grab the same item twice
 Dim check As Integer = -1
 
 If totalRowCount <= maxresults Then
 ' if its short, just pass the entire results back
 ' if its really short, activate the backup/default item
 passed = results
 If totalRowCount < maxresults Then
 backupI.Text = backupItem
 Else
 backupI.Text = String.Empty
 End If
 Else
 backupI.Text = String.Empty  ' no default/backup needed
 passed = New Collection(Of Catalog.Product) ' initialize returned var
 
 ' now grab MAXRESULTS items from that collection randomly
 For d As Integer = 1 To maxresults
 Dim x As Integer = check
 Do Until x <> check
 x = RandomNumber(0, totalRowCount)
 Loop
 check = x  'don't repeat items!  As collection, do until not x.contains(check)
 
 passed.Add(results.Item(x)) ' add the pulled item to returned var
 Next d
 End If
 DataList1.DataSource = passed  'set the repeater
 DataList1.DataBind()
 
 
 End Sub
 Private Function RandomNumber(ByVal MaxNumber As Integer, _
 Optional ByVal MinNumber As Integer = 0) As Integer
 
 Return randx.Next(MinNumber, MaxNumber)
 
 End Function
 
 
 Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles DataList1.ItemDataBound
 If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
 
 ' grab the product
 Dim p As Catalog.Product = CType(e.Item.DataItem, Catalog.Product)
 If p IsNot Nothing Then
 
 ' grab the link
 Dim destinationLink As String = Utilities.UrlRewriter.BuildUrlForProduct(p, Me.Page.Request)
 If destinationLink.StartsWith("/") Then
 destinationLink = "http://" & Request.ServerVariables("SERVER_NAME") & destinationLink
 End If
 
 ' grab the image url
 Dim imageUrl As String
 imageUrl = Page.ResolveUrl(Utilities.ImageHelper.GetValidImage(p.ImageFileSmall, True))
 If imageUrl.StartsWith("/") Then
 imageUrl = "http://" & Request.ServerVariables("SERVER_NAME") & imageUrl
 End If
 
 
 ' now build the item as an RSS item
 Dim sb As StringBuilder = New StringBuilder()
 
 sb.Append("      <item>" & vbCrLf)
 sb.Append("        <title>" & HttpUtility.HtmlEncode(p.ProductName) & "</title>" & vbCrLf)
 sb.Append("        <description>" & HttpUtility.HtmlEncode(p.ShortDescription) & "</description>" & vbCrLf)
 sb.Append("        <link>" & destinationLink & "</link>" & vbCrLf)
 sb.Append("        <guid>" & destinationLink & "</guid>" & vbCrLf)
 sb.Append("        <image>" & imageUrl & "</image>" & vbCrLf)
 sb.Append("      </item>" & vbCrLf)
 
 
 DirectCast(e.Item.FindControl("itemparams"), Literal).Text = sb.ToString()
 
 Else
 
 DirectCast(e.Item.FindControl("itemparams"), Literal).Text = String.Empty
 End If
 End If
 End Sub
 
 
 End Class
 
 |