Saturday 20 November 2010

Easy guide to databound RSS feeds in Asp.Net

 Ok, while I'm making sure the next part on the vertical datagrids makes sense I thought I'd drop in a quick bit on databound RSS feeds in asp.net


 I'd like to empasise first off this is in no way a method I can take credit for, but I wanted to pass it along to help out any dev's out there who get the fateful brief "oh, before we put this live can you just stick an RSS feed in"


 If you've done any reading then you'll know that RSS (Really Simple Syndication) is based around a specific XML format. The key to simple databound RSS is to use a standard page and standard controls, but writing out XML rather than HTML markup.


 To make the page work as a feed you need to do the following:


 -Delete out all preentered content except for the @Page directive


 -In the @Page directive include the attribute ContentType="text/xml"


 -Include a standard datasource that will output the data that you want to publish


 -Include an asp repeater with the DataSourceID set to your datasource, and with the following content:


HeaderTemplate:


 <rss version="2.0">
      <channel>
        <title>FEED NAME</title>
        <link>URL THAT THE FEED WILL BE PUBLISHED TO</link>
        <description>DESCRIPTION OF THE FEED</description>
       
FooterTemplate:


</channel>
</rss>


ItemTemplate:

        <item>
            <title><%#DataBinder.Eval(Container.DataItem, "POST TITLE COLUMN")%></title>
            <description><%#DataBinder.Eval(Container.DataItem, "POST TEXT SUMMARY COLUMN")%></description>
            <link>CLICK-THROUGH URL</link>
            <pubdate><%#DataBinder.Eval(Container.DataItem, "ARTICLE TIMESTAMP COLUMN")%></pubdate>
        </item>


That's all it takes - Enjoy :)

No comments:

Post a Comment