Skip to content

Instantly share code, notes, and snippets.

@warrenbuckley
Created January 31, 2012 12:17
Show Gist options
  • Save warrenbuckley/1710234 to your computer and use it in GitHub Desktop.
Save warrenbuckley/1710234 to your computer and use it in GitHub Desktop.
Umbraco V5 Example - Fetch Remote RSS
@inherits PartialViewMacroPage
@using Umbraco.Cms.Web.Macros
@* Added these in *@
@using System.ServiceModel.Syndication
@using System.Xml;
@{
// Get the remote url
var remoteUrl = String.IsNullOrEmpty(Model.MacroParameters.remoteURL) ? "http://feeds.feedburner.com/umbracoblog" : Model.MacroParameters.remoteURL;
// Get the web request
var request = WebRequest.Create(remoteUrl);
// Set the timeout to 3 seconds (3000ms)
request.Timeout = 3000;
// Let's put it in a try/catch because the web request could throw and error (timeout, 500, etc)
try
{
using (var response = request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
if (stream != null)
{
var reader = XmlReader.Create(stream);
var feed = SyndicationFeed.Load(reader);
foreach (SyndicationItem item in feed.Items)
{
var link = item.Links.FirstOrDefault().GetAbsoluteUri();
var description = item.ElementExtensions.ReadElementExtensions<string>("encoded", "http://purl.org/rss/1.0/modules/content/").FirstOrDefault();
var pubDate = item.PublishDate;
<div class="rssItem">
<h1><a href="@link">@item.Title.Text</a></h1>
<em>@pubDate.ToString("dd/MM/yy @ HH:mm")</em>
@Html.Raw(description)
</div>
}
}
}
}
}
catch (Exception ex)
{
// Log the error or do something with it...
}
}
@jlcmad
Copy link

jlcmad commented Jan 31, 2012

Hi Warren, I think I'd use a different approach here. I'd use the System.ServiceModel.Syndication namespace and would retrieve the data using the SyndicationFeed.Load method. I would also set a timeout and put the logic in a try/catch because the web request could throw and error (timeout, 500, etc). Example:

@inherits PartialViewMacroPage
@using Umbraco.Cms.Web.Macros
@using System.ServiceModel.Syndication
@using System.Xml;

@{
  // Get the remote url 
  var remoteUrl = String.IsNullOrEmpty(Model.MacroParameters.remoteURL) ? "http://feeds.feedburner.com/umbracoblog" : Model.MacroParameters.remoteURL;

  // Get the web request 
  var request = WebRequest.Create(remoteUrl);

  // Set the timeout to 3 seconds
  request.Timeout = 3000;

  // Let's put it in a try/catch because the web request could throw and error (timeout, 500, etc)
  try
  {
    using (var response = request.GetResponse())
    {
      using (var stream = response.GetResponseStream())
      {
        if (stream != null)
        {
          var reader = XmlReader.Create(stream);
          var feed = SyndicationFeed.Load(reader);
          foreach (SyndicationItem item in feed.Items)
          {
              var link = item.ElementExtensions.ReadElementExtensions<string>("origLink", "http://rssnamespace.org/feedburner/ext/1.0").FirstOrDefault();
              var description = item.ElementExtensions.ReadElementExtensions<string>("encoded", "http://purl.org/rss/1.0/modules/content/").FirstOrDefault();
              var pubDate = item.PublishDate;
              <div class="blogItem">
              <h1><a href="@link">@item.Title.Text</a></h1>
              <em>@pubDate.ToString("dd/MM/yy @ HH:mm")</em>
              @Html.Raw(description)
              </div>
          }
        }
      }
    }
  }
  catch (Exception ex)
  {
      // Log the error
  }
}

@warrenbuckley
Copy link
Author

@jorgelusar thanks for your much better version using the .NET 4 SyndicationClass. I have updated the example.

@jlcmad
Copy link

jlcmad commented Feb 1, 2012

@warrenbuckley Actually thank YOU for all your help and for all this examples :) #h5yr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment