Adding a List of Recent Posts To The Silicon Valley Code Camp Home Page (From RSS Feed)

There are 10,000 ways to do this.  Back way back when (ASP.NET 1.1) days, we would have created either a custom control to do this, then added a Repeater control on the page to display it.  Well, the Silicon Valley Code Camp Website was started back in those days and since what I want is so simple, I decided to do just that (minus the custom control).  I really only wanted to spend about 10 minutes doing this, and as it turned out, it took about 30.  Oh well, 3x plan verses execution.  Could do better next time.

So, here is basically what I want on the left side bar of the home page of Silicon Valley Code Camp:

RSS Feed Look

 

First, I’ll attach my simple project I build to do this standalone so you can run it yourself, then, I’ll do a quick step through of the key code. 

All that is really in this project is a simple c# class file that contains a business object and one get method to retrieve the list.  That file is called RSSFeed.cs.  I’ve pasted all the code of that class below:

using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Xml.Linq;

namespace App_Code
{
[DataObject(true)]
public class RSSFeedObject
{

[DataObjectMethod(DataObjectMethodType.Select, true)]
public List<RSSItem> Get(int numberToGet)
{
XDocument feedXML = XDocument.Load
("http://blog.siliconvalley-codecamp.com/feed/");

var feeds = feedXML.Descendants("item").
Select(feed => new
{
PostTitle = feed.Element("title").Value,
PostURL = feed.Element("link").Value
});
var rssItems = new List<RSSItem>();
int id = 0;
foreach (var rec in feeds)
{
rssItems.Add(new RSSItem
(id,rec.PostTitle,rec.PostURL));
id++;
if (id >= numberToGet)
{
break;
}
}
return rssItems;
}

}

public class RSSItem
{
[DataObjectField(true)]
public int Id { get; set; }

[DataObjectField(false)]
public string PostTitle { get; set; }

[DataObjectField(false)]
public string PostURL { get; set; }

public RSSItem(int id, string postTitle,
string postURL)
{
Id = id;
PostTitle = postTitle;
PostURL = postURL;
}
}
}

Then, in the Default.aspx page, you can simply reference this object with a Repeater and an ObjectDataSource and you are done!  That is it, very simple.  Below is the page code.

<asp:ObjectDataSource ID="ObjectDataSourceRSS" runat="server"
SelectMethod="Get" TypeName="App_Code.RSSFeedObject">
<SelectParameters>
<asp:Parameter DefaultValue="5" Name="numberToGet" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:Repeater ID="RepeaterRSS" runat="server" DataSourceID="ObjectDataSourceRSS">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
<ItemTemplate>
<li>
<asp:HyperLink ID="HyperLinkURL" runat="server"
NavigateUrl='<%# Eval("PostURL") %>'
Text='<%# Eval("PostTitle") %>'></asp:HyperLink>
</li>
</ItemTemplate>
</asp:Repeater>

I’m sure someone is going to point out how this could have been done in three lines of code.  Until then, I’ll keep this in the Silicon Valley Code Camp site.

BTW, if the site looks different, it’s because a wizard html/css guy has come by and cleaned it up to look good. What you see in the top screen shot is what it looks like without any formatting.  Just unordered list and listitems with no formatting.

HTH’s!

About Peter Kellner

Peter is a software professional specializing in mobile and web technologies. He has also been a Microsoft MVP since 2007. In addition, he's a multi-course video author at Pluralsight. To read more about Peter Kellner and his experience click here. For information about how Peter Kellner might be able to help you with your project click here.

Follow me:


Follow

Get every new post delivered to your Inbox

Join other followers: