Adding an RSS Feed to Sharepoint Using a Web Part

Note: This tutorial assumes at least basic knowledge of creating and deploying SharePoint Solutions using Visual Studio

A common request I have seen crop up a number of times when working with SharePoint is to add an RSS feed to the site. There are a handful of SharePoint Apps out there on the SharePoint store that will do this for you, but I wanted to take a look at coding this myself and packaging it as a custom web part. After doing a little digging this turned out to be fairly straight forward. RSS feeds are obviously pretty easy to work with, so it was really just a matter of finding the most efficient methods.

Some of you may be familiar with the System.ServiceModel.Syndication Namespace that is part of the .NET library. This namespace contains some really useful classes designed to work with RSS feeds. For this example, we are going to take a look at the SyndicationFeed and SyndicationFeedItem classes with this namespace.

Open up Visual Studio and create a new empty SharePoint project. Click OK and then choose to deploy as a farm solution. For this example we will use a farm solution so that we can deploy a Visual Web Part to make the code a bit cleaner or more organized. This tutorial could easily be adapted to a Sandbox solution with a standard web part – you’ll just have to build the HTML in the code behind file by overriding the RenderControls method of the web part.

After your new solution is created, right click on the project name and choose to add a new item. Select Visual Web Part, name it NewsWebPart or something similar and choose add.

Double click on NewsWebPart.ascx and paste the simple code below into the file below all the auto generated code. This is the control that will actually get rendered out onto the page. We are going to use a literal control to do a bit of string building in our code behind file.

<div id="news">
    <h1 class="news-header">News</h1>
    <asp:Literal ID="ltNews" runat="server"></asp:Literal>
  </div>

Switch over to NewsWebPart.ascx.cs and paste the code below into the Page_Load method.

string url = "http://news.yahoo.com/rss/";
XmlReader reader = XmlReader.Create(url);
SyndicationFeed feed = SyndicationFeed.Load(reader);
reader.Close();
Collection<SyndicationItem> items = (Collection<SyndicationItem>)feed.Items;
string newsText = "";
for (int i = 0; i < 7; i++)
{
    Collection<SyndicationLink> content = items[i].Links;
    newsText += "<div class=\"news-item\"><a class=\"national-news-title\" target=\"_blank\" href=\"" + content[0].Uri + "\">" + items[i].Title.Text + "</a></div>";
}
nationalNews.Text = newsText;

Lets step through this code.

First we declare a string variable to hold the url to our RSS feed. If you visit this URL in your browser you will see that it is indeed an XML RSS feed – I arbitrarily chose one from Yahoo.com since it’s pretty straight forward.

Next we instantiate a new XMLReader by passing in the URL to our XML feed.

Next we create a new SyndicationFeed object, which is designed to handle RSS feeds. We use the load method to import our XMLReader and then close the reader object.

If you visit the RSS feed URL you can see that the XML contains a list of “Item” elements.  We want to acquire a collection of all of these items in our RSS feed, so we create a Collection of SyndicationItem, each of which will hold one element of our RSS feed.

Next we’ll declare a variable that will hold the HTML we are going to write into our literal control.

We can now loop through the collection of SyndicationItems. Inside the loop we declare a second collection of the SyndicationLink class, which will receive all of the links that have been included in each item of the RSS feed. We can then build a series of links using some HTML string concatentation.

Finally we set the text of our literal to the HTML we built so that a list of links gets rendered out on the page.

If you want more than just the links to the RSS articles, you can also use the Summary.Text property of the each item as you loop through.

Now if we click run to deploy our solution, we can see it available in the list of custom web parts. Add it to your page and you should see an RSS feed displayed.

webpart-1024x576

A couple other points to think about – you might want to add an editable property to the Web part to allow the user to enter different URLs for their RSS feed. Another idea would be to include an option for how many items to display. We chose 7, but you could certainly make this adjustable. This tutorial isn’t really about building web parts, so we’ll skip those steps for now, but feel free to play around with some other ideas.

No Comments

Comments Closed