Thursday, May 31, 2012

Custom RSS feed Display


Really Simple Syndication (RSS) is a widely used technology for transmitting information across the Internet and intranets. Many Web sites offer RSS Feeds you can subscribe to so you can get the latest information from the source automatically. You can also subscribe to RSS Feeds from SharePoint libraries, lists, and other elements, so you can see when someone adds a document or changes a list item.

You can use a RSS Viewer Web Part to display an RSS Feed on a SharePoint site. One can also keep track of changes in libraries, calendars, and lists on other SharePoint sites. Sharepoint 2010 has provided with a great OOB web part to display the RSS feed and one can modify it for stying and customize it per requirements. more information on how to set this up can be found at  here

I had a requirement to put the RSS feed as an output in a webpart and associate it to the master page.

To get around the credentials I had to use the class XmlUrlResolver

Here is a sample of a function which uses the class SyndicationFeed   and reads the RSS via XML reader, to get the active items only.
   protected void ReadRSS(string ListRssUrl)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    XmlReaderSettings settings = new XmlReaderSettings();

                    // Create a resolver with default credentials.
                    XmlUrlResolver resolver = new XmlUrlResolver();
                    resolver.Credentials = 

                System.Net.CredentialCache.DefaultCredentials;

                    // Set the reader settings object to use the resolver.
                    settings.XmlResolver = resolver;


                    // Create the XmlReader object.
                    XmlReader reader = XmlReader.Create(ListRssUrl, settings);

                    var feed = SyndicationFeed.Load(reader);

                    string strCategory = string.Empty;
                    StringBuilder sb = new StringBuilder();
                    if (feed == null)
                    {
                        sb.Append("No Feeds in RSS");
                        Div1.InnerHtml = sb.ToString();

                    }
                    else
                    {

                       
                        foreach (SyndicationItem item in feed.Items)
                        {
                            var category = item.Categories;
                            foreach (var cat in category)
                            {
                                // get the active category items only...
                                if (cat.Name == "Active")
                                {

                                    foreach (var link in item.Links)
                                    {
                                      RenderRSS(item.Title.Text,link.Uri.ToString(),sb);

                                    }
                                }
                            }

                        }
                        Div1.InnerHtml = sb.ToString();
                    }


                });
            }
            catch (Exception e)
            {
                //exception handling
            }
        }
  private void RenderRSS(string strRSSTitle, string strRSSLink, StringBuilder sb)
        {
            sb.Append("<li>"); // Begin  Li for each RSSFeedURL feed
            sb.Append("<a href="+"'"+strRSSLink+ "'" + ">"+strRSSTitle+"</a>"); 
            sb.Append("</li>"); //End Li for each RSSFeedURL feed
        }

No comments:

Post a Comment