Microsoft Feeds API, a Simple List Extensions (SLE) sample
I know I promised an updated version of the really simple sample, but I got hooked on the Simple List Extensions. So first I'm going to give a small sample on SLE combined with the Microsoft Feeds API. In our company we develop call center solutions and a small part of the application is a scoreboard, this scoreboard gives the scores of all projects and agents, how good is a project and what is an agent scoring. These scores are calculated by an application and every project as a custom algorithm, some of the calculations take minutes to finish since historical data is sometimes needed. At this moment this application stores the scores in a sql table, another application the scoreboard loops through the table and showes the scores in a graphical manner. This way you can start multiple scoreboards without more load on the sql databases. At this time managers asked too see the the scores at home, "get it online" as they say. Well this is where SLE comes in play, generate an rss feed from the calculator application and your done, the scoreboard application only uses the rss feeds as source. No impact on the sql server and rss is readable without a custom application from any location.
Great, the following sample is a prove of concept and I wanted to share this with you all.
First of all we need a feed, just put an xml file on a webfolder and you have a feed. the following is an example of such an xml file.
It looks like a regular rss file but we added the cf:treatAs element, this element informs the Rss Platform Engine that this rss stream is a list. More over we can add additional sorting with the cf:sort element. The rss feed now looks like this in IE7, we see the sorting in action through the "The Agent" and "The Score" buttons.

Of course this is all great, but what if we wanted to use this feed in a custom application. As an example I will show you how to load the items in a listbox, and what is more the listbox will be updated every time the feed is downloaded. The application contains two major methods "BindFeedEvents" and "UpdateScoreBoard". First of all we are going to look at the UpdateScoreBoard method, this method loads the feed and gets the items of the feed, once it has an item it reads the score and agent elements from the scoreboard namespace. In order to read these items we need to read the xml stream from the item, there is no way to get the elements (why not ? This would be a great feature on the next release of the Feeds API). We load the xml in a document and select the nodes through an XPath expression, do not forget to add a NamespaceManager because we are using a different namespace than the default namespace. Here's the code.
private void UpdateScoreBoard() {
ListBox1.Items.Clear();
IFeedsManager manager = new FeedsManagerClass();
IFeedFolder rootFolder = (IFeedFolder)manager.RootFolder;
IFeed feed = (IFeed)rootFolder.GetFeed("ScoreBoard Project1");
foreach (IFeedItem item in (IFeedsEnum)feed.Items) {
XmlDocument document = new XmlDocument();
document.LoadXml(item.Xml(FEEDS_XML_INCLUDE_FLAGS.FXIF_NONE));
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(document.NameTable);
namespaceManager.AddNamespace("scoreboard", "http://www.widec.be/blog/rss/samples/scoreboard");
XmlNode scoreNode = document.SelectSingleNode("item/scoreboard:score", namespaceManager);
XmlNode agentNode = document.SelectSingleNode("item/scoreboard:agent", namespaceManager);
if (scoreNode != null && agentNode != null) {
ListBox1.Items.Add(agentNode.InnerText + " - " + scoreNode.InnerText);
}
}
}
Since we want the listbox to be updated once the Rss Platform Engine refreshes the feed we need to bind events on the feed. We do this like I previously showed in my post "Microsoft Feeds API, event handlers", if you want an example in VB.Net there is one on the RSS team blog called Events in VB.NET. Again I have declared an IFeedEvents_Event interface on the form, this interface will hold the eventwatcher from the feed. How this is done is showed in the following code.
private void BindFeedEvents() {
IFeedsManager manager = new FeedsManagerClass();
IFeedFolder rootFolder = (IFeedFolder)manager.RootFolder;
IFeed feed = (IFeed)rootFolder.GetFeed("ScoreBoard Project1");
FeedEvents = (IFeedEvents_Event)feed.GetWatcher(FEEDS_EVENTS_SCOPE.FES_SELF_ONLY,
FEEDS_EVENTS_MASK.FEM_FEEDEVENTS);
FeedEvents.FeedDownloadCompleted +=
new IFeedEvents_FeedDownloadCompletedEventHandler(Feed_FeedDownloadComplete);
}
All we need to do now is declare an event handler to execute the code we want and we want the scoreboard to be updated. Here's the handler.
private void Feed_FeedDownloadComplete(string path, FEEDS_DOWNLOAD_ERROR error) {
UpdateScoreBoard();
}
After the InitializeComponent call in the constructor of the form you place a call to BindFeedEvents method and when you start the form you are instantly connected with updates on the feed. The complete source of the application is availlable here.
When coding this sample I had the following remarks.
- Why is the Feeds API not managed code ? This would be much easier to code and hey we live in 2006, why create solutions in COM ?
- Why is there no way to get custom elements out of the items, in a managed assembly this would be as easy as 1 + 1 = 2, as I showed in this sample.
- The possibilities of the feeds API go way beyond reading posts of blogs. It's a whole new way of thinking and developing, a big thanks to the RSS team, great work !!!