Category Archives: SharePoint

Creating an RSS Feed from a SharePoint 2010 Web Part

I’ve got a simple SharePoint web part which displays news items based on an external data source and the requirement is to build an RSS2 feed which also included some custom image tags similar to the Channel 9 RSS feed. The out the box RSS feeds aren’t valid as we are dealing with a custom data source, but even if they were, they wouldn’t have the custom tags included. So here is what I did.

All the existing code for the web part I left as is, but I added an if statement to check for GET parameters in the URL. So if the URL looks like then the RSS feed is displayed, otherwise it’s just the same web part as it was before. This all happens under the protected void Page_Load(object sender, EventArgs e) method in the web part.

if (Request.QueryString["feed"] != null && Request.QueryString["feed"] == "rss2")

Then I go ahead and write the feed. First we clear out anything the page has already generated. Response.Clear();

Next we construct our XML document conforming to RSS standards. Note that I am using the “media” namespace used by the channel 9 RSS feed which give me options for using image/thumbnail tags.

Response.ContentType = "text/xml";
XmlTextWriter feedWriter
	= new XmlTextWriter(Response.OutputStream, Encoding.UTF8);


// These are RSS Tags
feedWriter.WriteAttributeString("version", "2.0");
feedWriter.WriteAttributeString("xmlns", "media", null, "");

feedWriter.WriteElementString("title", "News");
feedWriter.WriteElementString("link", PANewsUrl);
feedWriter.WriteElementString("description", "News RSS Feed");
	String.Format("Copyright {0} My Company. All rights reserved.",DateTime.Now.Year));

Then Trigger your business logic to get the articles from wherever your data source, e.g. a database or WCF service. This bit will change depending on the service you are writing. In my example I’m returning a list of “Articles” from a web service.

Articles = Article.GetArticles(strSection, 20, Mode, "MainSection");

Then I loop through the list of Articles and add the nodes to my XMLWriter object. Note the custom image tags used which enable thumbnails and main images to be displayed in the feed. This is very useful for visually rich clients like mobile apps.

foreach (Article post in Articles)
	feedWriter.WriteElementString("title", post.Header);
	feedWriter.WriteElementString("description", post.Content + post.ContentDetail);
	feedWriter.WriteElementString("link", post.ArticleUrl);
	if (!String.IsNullOrEmpty(post.ImageThumbnailUrl))
		feedWriter.WriteAttributeString("url", post.ImageThumbnailUrl);
	if (!String.IsNullOrEmpty(post.ImageUrl))
		feedWriter.WriteAttributeString("url", post.ImageUrl);
		feedWriter.WriteAttributeString("medium", "image");

Finally I close all the tags and end my response.

// Close all open tags tags

Now when I deploy and navigate to any SharePoint page containing my web part and append the GET parameters ?feed=rss2, an RSS feed is displayed instead of the normal content.