I am trying to consume and parse an RSS feed from a SharePoint 2010 list. I can gain access to the list through an RSS feed, but I cannot parse the data. The feed reader works with other feeds not from SharePoint. I have narrowed down my problem, I think,
to CDATA sections in the XML.
There are tons of articles about creating the RSS or consuming with a WebPart, but I want to consume it OUTSIDE of SharePoint using ASP.NET (2010, 4.0).
Funny thing is if I use localhost account to test the page in VS 2010 it will read the feed and give me all BUT the CDATA. If I publish the page to a live server, I cannot consume the feed. No errors, but nothing is listed. I am including the code. I would
prefer a solution in VB.
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Feed1.Text = ProcessRSS("feed URL", "Events") ' this is a Literal control I write the results to
End Sub
Public Shared Function ProcessRSS(ByVal rssURL As String, ByVal feed As String) As String
Dim request As WebRequest = WebRequest.Create(rssURL)
request.Credentials = CredentialCache.DefaultCredentials()
Dim response As WebResponse = request.GetResponse()
Dim sb As New StringBuilder("")
Dim rssStream As Stream = response.GetResponseStream()
Dim rssDoc As New XmlDocument()
rssDoc.Load(rssStream)
Dim rssItems As XmlNodeList = rssDoc.SelectNodes("rss/channel/item")
Dim title As String = ""
Dim link As String = ""
Dim description As String = ""
Dim upperlimit As Integer = rssItems.Count
If upperlimit > 5 Then
upperlimit = 5
End If
If upperlimit > 0 Then
sb.Append("<p>" + feed + "</p><ul>")
Dim i As Integer = 0
While i < upperlimit
Dim rssDetail As XmlNode
rssDetail = rssItems.Item(i).SelectSingleNode("title")
'If rssDetail <> Nothing Then
title = rssDetail.InnerText
' title = "This is title - " & i.ToString
'Else
' title = ""
'End If
rssDetail = rssItems.Item(i).SelectSingleNode("link")
'If rssDetail <> Nothing Then
link = rssDetail.InnerText
'Else
' link = ""
'End If
sb.Append("<li><a href='" + link + "' target='_blank'>" + title + "</a></li>")
rssDetail = rssItems.Item(i).SelectSingleNode("description")
'If rssDetail <> Nothing Then
description = rssDetail.InnerText
'Else
' link = ""
'End If
sb.Append("<li><a href='" + link + "' target='_blank'>" + title + "</a><br/>" + description + "</li>")
i += 1
End While
sb.Append("</ul>")
End If
Return sb.ToString()
End Function
This is the RSS feed content.
<?xml version="1.0" encoding="UTF-8"?>
<!--RSS generated by Microsoft SharePoint Foundation RSS Generator on 2/26/2013 8:27:43 AM -->
<?xml-stylesheet type="text/xsl" href= URL to site" version="1.0"?>
<rss version="2.0">
<channel>
<title>Events Management: Events</title>
<link>https://myshare.tcu.edu/studentactivites/events/Lists/Events/Default View.aspx</link>
<description>RSS feed for the Events list.</description>
<lastBuildDate>Tue, 26 Feb 2013 14:27:43 GMT</lastBuildDate>
<generator>Microsoft SharePoint Foundation RSS Generator</generator>
<ttl>60</ttl>
<language>en-US</language>
<image>
<title>Events Management: Events</title>
<url>https://myshare.tcu.edu/studentactivites/events/_layouts/images/siteIcon.png</url>
<link>https://myshare.tcu.edu/studentactivites/events/Lists/Events/Default View.aspx</link>
</image>
<item>
<title>Mother's Monster Jam</title>
<link>https://myshare.tcu.edu/studentactivites/events/Lists/Events/DispForm.aspx?ID=8</link>
<description><![CDATA[<div><b>Title:</b> Mother's Monster Jam</div>
<div><b>EventName:</b> Mother's Monster Jam</div>
<div><b>EventType:</b> <a onclick="OpenPopUpPage('https://myshare.tcu.edu/studentactivites/events/_layouts/listform.aspx?PageType=4&ListId={D226EEAF-75CF-4059-B2A4-CF02DBF0FC6A}&ID=1&RootFolder=*', RefreshPage); return false;" href="https://myshare.tcu.edu/studentactivites/events/_layouts/listform.aspx?PageType=4&ListId={D226EEAF-75CF-4059-B2A4-CF02DBF0FC6A}&ID=1&RootFolder=*">Concert</a></div>
<div><b>Organization:</b> <a onclick="OpenPopUpPage('https://myshare.tcu.edu/studentactivites/events/_layouts/listform.aspx?PageType=4&ListId={04A59E33-E015-491C-B9D0-26CABF8550E2}&ID=12&RootFolder=*', RefreshPage); return false;" href="https://myshare.tcu.edu/studentactivites/events/_layouts/listform.aspx?PageType=4&ListId={04A59E33-E015-491C-B9D0-26CABF8550E2}&ID=12&RootFolder=*">Alpha Psi Omega</a></div>
<div><b>StartDate:</b> 2/28/2013</div>
<div><b>EndDate:</b> 2/28/2013</div>
<div><b>StartTime:</b> 2/28/2013 12:00 AM</div>
<div><b>EndTime:</b> 2/28/2013 1:00 AM</div>
<div><b>EventDescription:</b> fsda fasdf asdf </div>
<div><b>EmployeeID:</b> 999,999,999</div>
<div><b>Event Image:</b> <a href="http://www.dailymobile.net/wp-content/uploads/2012/05/windows-phone-480x800-wallpaper-387.jpg">Mother's Monster Jam</a></div>
<div><b>Post to Twitter:</b> No</div>
<div><b>Post to What To Do:</b> No</div>
<div><b>Location:</b> <a onclick="OpenPopUpPage('https://myshare.tcu.edu/studentactivites/events/_layouts/listform.aspx?PageType=4&ListId={671BDF74-C0AA-44FA-9444-7211F9C00185}&ID=3&RootFolder=*', RefreshPage); return false;" href="https://myshare.tcu.edu/studentactivites/events/_layouts/listform.aspx?PageType=4&ListId={671BDF74-C0AA-44FA-9444-7211F9C00185}&ID=3&RootFolder=*">Union Ballroom</a></div>
<div><b>Created By:</b> Larremore, Jef</div>
]]></description>
<author>Larremore, Jef</author>
<pubDate>Tue, 26 Feb 2013 14:19:45 GMT</pubDate>
<guid isPermaLink="true">URL to Link</guid>
</item>
</channel>
</rss>
I have managed to come up with a different way to accomplish what you are trying to accomplish, and in a more concise way. It also seems to overcome the probelm you may be having with CDATA.
You need to include reference to System.ServiceModel so that you can use the SyndicationFeed class provided in .NET 4.0 and above.
Here is the function:
Public Shared Function ProcessRSS(ByVal rssURL As String, ByVal feed As String) As String
Dim request As WebRequest = WebRequest.Create(rssURL)
request.Credentials = CredentialCache.DefaultCredentials()
Dim response As WebResponse = request.GetResponse()
Dim sb As New StringBuilder("")
Dim rssStream As Stream = response.GetResponseStream()
Dim reader As XmlReader = XmlReader.Create(rssStream)
Dim syfeed As SyndicationFeed = SyndicationFeed.Load(reader)
Dim syndItems = (From item In syfeed.Items
Select item).ToList().Take(5) 'only take max 5 items.
Dim title As String = ""
Dim link As String = ""
Dim description As String = ""
sb.Append("<p>" + feed + "</p><ul>")
For Each item As SyndicationItem In syndItems
title = item.Title.Text
link = item.Links(0).Uri.AbsoluteUri
description = item.Summary.Text
' Do string building according to these values...
Next
Return sb.ToString()
End Function
Also note that you will need to import System.ServiceModel.Syndication.
Hopefully this'll work.
EDIT: By the way, for future reference, this method works with Atom 1.0 feeds as well. The SyndicationFeed object understands both formats.
Awesome. I needed a little tweaking, but overall right on the money. I need to expand on the CDATA section a little though. The data in the CDATA ends up looking like this and I need to assign the values to variables. This is string manipulation but I'm
not quite clear how to look for and iterate through the list. Each element ends with a colon so I know I can find that. I was thinking of using Left() or Mid() or Right() but there has to be a way to cycle through it given that there is a standard delimiter.
EventName: Mother's Monster Jam
EventType: Concert
Organization: Alpha Psi Omega
StartDate: 2/28/2013
EndDate: 2/28/2013
StartTime: 2/28/2013 12:00 AM
EndTime: 2/28/2013 1:00 AM
EventDescription: fsda fasdf asdf
EmployeeID: 999,999,999
Event Image: Mother's Monster Jam
Post to Twitter: No
Post to What To Do: No
Location: Union Ballroom
Member
11 Points
48 Posts
Consuming and Parsing an RSS Feed From SharePoint 2010
Feb 26, 2013 12:01 PM|jlarremore|LINK
I am trying to consume and parse an RSS feed from a SharePoint 2010 list. I can gain access to the list through an RSS feed, but I cannot parse the data. The feed reader works with other feeds not from SharePoint. I have narrowed down my problem, I think, to CDATA sections in the XML.
There are tons of articles about creating the RSS or consuming with a WebPart, but I want to consume it OUTSIDE of SharePoint using ASP.NET (2010, 4.0).
Funny thing is if I use localhost account to test the page in VS 2010 it will read the feed and give me all BUT the CDATA. If I publish the page to a live server, I cannot consume the feed. No errors, but nothing is listed. I am including the code. I would prefer a solution in VB.
Thanks!
Jef
Member
120 Points
23 Posts
Re: Consuming and Parsing an RSS Feed From SharePoint 2010
Feb 28, 2013 09:18 AM|danellos|LINK
Hey there.
I have managed to come up with a different way to accomplish what you are trying to accomplish, and in a more concise way. It also seems to overcome the probelm you may be having with CDATA.
You need to include reference to System.ServiceModel so that you can use the SyndicationFeed class provided in .NET 4.0 and above.
Here is the function:
Also note that you will need to import System.ServiceModel.Syndication.
Hopefully this'll work.
EDIT: By the way, for future reference, this method works with Atom 1.0 feeds as well. The SyndicationFeed object understands both formats.
Member
11 Points
48 Posts
Re: Consuming and Parsing an RSS Feed From SharePoint 2010
Mar 05, 2013 11:22 AM|jlarremore|LINK
Awesome. I needed a little tweaking, but overall right on the money. I need to expand on the CDATA section a little though. The data in the CDATA ends up looking like this and I need to assign the values to variables. This is string manipulation but I'm not quite clear how to look for and iterate through the list. Each element ends with a colon so I know I can find that. I was thinking of using Left() or Mid() or Right() but there has to be a way to cycle through it given that there is a standard delimiter.