Menu

Search

InstantASP Support

Help & Support


Consume a InstantKB or InstantForum RSS Feed


Common Problems

We've received a number of email's recently asking how to display a InstantKB or InstantForum RSS feed within an existing web site or web application. Based on this feedback i decided to spend a little time preparing a basic example which will hopefully assist others.

In this example we will display the InstantKB latest articles RSS feed from our own knowldgebase at...

https://kb.instantasp.co.uk/LatestRss/

If you wish to consume a RSS feed for search results the RSS endpoint would be...

https://kb.instantasp.co.uk/SearchRss.aspx?Keywords=InstantKB

You would of course need to replace our URL with your InstantKB URL.

You can easily adapt the example to work with any RSS feed exposed by InstantKB or InstantForum by simply modifying the RSS feed URL used within the example code. the complete code example is provided within a ZIP file at the bottom of this article.

Displaying the Feed (Default.aspx)

We'll use a standard ASP.NET datalist to display the contents of our feed. We've done this to keep things simple however you could easily modify the example to work with any databound control. The datalist markup can is shown below...

<asp:DataList id="dlRSS" RepeatLayout="Flow" RepeatDirection="Vertical" Width="100%"
Runat="Server">
    <ItemTemplate>
          <div style="float: left; width: 100%; font-size: 10pt; line-height: 22px;">
               <div style="float: left;">
                    <asp:HyPerLink id="hypTitle" runat="server" />
               </div>
               <div style="float:right;">
                    <asp:Label id="lblDate" Runat="Server" />
               </div>
          </div>
    </ItemTemplate>
</asp:DataList>

Populating the DataList (Default.aspx.vb)

To populate this list with results from our InstantKB latest articles feed we use the following code...

PAge_Load Event

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

     Call BindRssFeed()

End Sub

BindRssFeed Method

The BindRSsFeed method binds our datasource (the RSS feed) to the datalist. We call the GetRssDataMethod to return a datatable of the RSS feed items. We them use a PagedDataSource object to limit the number of items displayed. In this example we only display the last 5 items.

Private Sub BindRssFeed()

     ' url to our RSs feed
     Dim strRSSURL As String = "https://kb.instantasp.co.uk/LatestRss/"

     ' get data table containing RSS data
     Dim dt As System.Data.DataTable = GetRSSDataTable(strRSSURL)

     ' used the pageddatasource object to restrict the number of itesm displayed
     Dim pgds As New PagedDataSource
     pgds.DataSource = dt.Rows
     pgds.PageSize = 5 ' how many items to display
     pgds.AllowPaging = True

     ' set data source
     dlRSS.DataSource = pgds

     ' bind data
     dlRSS.DataBind()

End Sub

GetRssDataTable Method

This is the method that actually calls the RSS feed and returns the feed as a datatable. You could improve this example by caching the datatable. We've opted to keep this example simple and as such have not included any code to handle if a RSS feed is unavailable.

Function GetRSSDataTable(ByVal strURL As String) As DataTable

     ' get the XML data
     Dim reader As System.Xml.XmlTextReader = New System.Xml.XmlTextReader(strURL)

     'return a new DataSet
     Dim ds As DataSet = New DataSet()
     ds.ReadXml(reader)

     ' return elements table
     Return ds.Tables(2)

End Function

ItemDataBound Event (for our datalist)

Here we simply populate the controls within the datalist with the datareturned from the feed. In this example we only display the feed title and created date however other fields could be displayed such as the "description" field which actually contains the feed item content.

Protected Sub dlRSS_ItemDataBound(ByVal sender As Object,_
ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlRSS.ItemDataBound

     If e.Item.ItemType = ListItemType.Item Or _
    e.Item.ItemType = ListItemType.AlternatingItem Then

         ' get data row
         Dim drView As System.Data.DataRow = _
         DirectCast(e.Item.DataItem, System.Data.DataRow)

         ' setup article hyperlink
         Dim hypTitle As System.Web.UI.WebControls.HyperLink = _
         DirectCast(e.Item.FindControl("hypTitle"), System.Web.UI.WebControls.HyperLink)

         ' populate feed item title
         If Not drView.Item("Title") Is System.DBNull.Value And _
         Not drView.Item("Title") Is Nothing Then _
         hypTitle.Text = drView.Item("Title").ToString()

         ' populate feed item URL
         If Not drView.Item("Link") Is System.DBNull.Value And _
         Not drView.Item("Link") Is Nothing _
         Then hypTitle.NavigateUrl = drView.Item("Link").ToString()

         ' setup modified date label
         Dim lblDate As System.Web.UI.WebControls.Label = _
         DirectCast(e.Item.FindControl("lblDate"), System.Web.UI.WebControls.Label)

         ' populate feed item date
         If Not drView.Item("pubDate") Is System.DBNull.Value And _
         Not drView.Item("pubDate") Is Nothing Then
              lblDate.Text = CType(drView.Item("pubDate"), System.DateTime).ToString("d")
         End If

     End If

End Sub

That's It!

This code should provide a good starting point for consuming RSS feeds within your own ASP.NET web sites & web applications from either InstantForum or InstantKB You can find a complete working example of this code attached below.

Once running successfully you will see a list similar to the screen grab below...



I hope this basic example helps. Please feel free to leave any comments below or contact us.


Attachments


ConsumeRSSFeed.zip 2.00 KB, 1.8K views

Optionally provide private feedback to help us improve this article...

Thank you for your feedback!


Comments

Member Photo
0
jkirklen posted 8 Years Ago
New Member with 3 recognition pointsNew Member with 3 recognition pointsNew Member with 3 recognition pointsNew Member with 3 recognition pointsNew Member with 3 recognition pointsNew Member with 3 recognition pointsNew Member with 3 recognition pointsNew Member with 3 recognition points
When trying this code I am getting an error "Expected DTD markup not found..."<br />
I think that it is looking for DTD for RSS, where should this DTD be located or where should it be referred to?

Comments require login or registration.

Details

Product: InstantKB, InstantForum
Version: All Versions
Type: HOWTO
Level: Intermediate
Article not rated yet.
Article has been viewed 25K times.
Last Modified: 5 Years Ago
Last Modified By: Ryan Healey

Options

Similar Articles


Tags