Tuesday, May 10, 2016

Accessing items from a Sitecore RSS feed in code

Creating RSS feeds of your Sitecore content is a great way to expose content to web site users, however it's also another way which you can access the data in code. Take news content for example, instead of accessing Sitecore items to display a news feed on a home page, the RSS feed could be used as the datasource.

This is done using System.ServiceModel.Syndication and allows you to specify a Sitecore feed URL and then access the items contained within. The code for that is relatively simple:

XmlReader reader = XmlReader.Create("http://test/feed");
SyndicationFeed sFeed = SyndicationFeed.Load(reader);
reader.Close();

foreach (SyndicationItem item in sFeed.Items)
{
    var title = item.Title.Text;
    var summary = item.Summary.Text;
    var link = item.Links.FirstOrDefault();
}

It's worth noting that the URL to the feed needs to include the server URL and not be relative.

This is also a great way to expose Sitecore content to third party systems (perhaps a SharePoint web part which displays the latest news on a web site for example). You can also get creative by having web forms for marketers create items in a location (that has an RSS feed) and then have a third party system read that feed...

No comments:

Post a Comment