Gabe Sumner

Gabe Sumner

The RSS Feed URL cannot be found!
The remote server returned an error: (400) Bad Request.

Back to all posts

Creating an RSS Feed UserControl in Sitefinity

If there is one thing blogging has taught me, it's that coming up with original content & ideas is hard work.  So I decided to do what everyone else is doing and simply use RSS feeds to artifically inflate the size of my web site.  In this way I can generate tons of content for my web site without having to do a lot of time-consuming "work".

RSS is the wave of the future and I predict in 5 years the Internet will consist of roughly 100 unique pages.  These pages will then be re-syndicated via RSS several billion times.  Rock on!!!  Let's get started...


This article describes how to create a simple Sitefinity UserControl for consuming an RSS 2.0 feed.  However, although I'm calling this a "Sitefinity UserControl" it is important to note that 95% of this code has nothing to do with Sitefinity.  Sitefinity can utilize any ASP.NET UserControl, it is merely a matter of making Sitefinity aware of the control's existence. 

Step 1: Create the UserControl

Create the following user control file: ~/UserControls/RssFeed.ascx  

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="RssFeed.ascx.cs" Inherits="UserControls_RssFeed" %> 
<asp:Repeater ID="RssRepeater" Visible="false" EnableViewState="false" runat="server">  
    <ItemTemplate> 
        <h2><asp:HyperLink ID="link" NavigateUrl='<%# DataBinder.Eval (Container.DataItem, "link") %>' EnableViewState="false" runat="server"><%# DataBinder.Eval (Container.DataItem, "title") %></asp:HyperLink></h2>  
        <div><asp:Literal ID="pubDate" Text='<%# DataBinder.Eval (Container.DataItem, "pubDate") %>' Visible="false" EnableViewState="false"  runat="server" /></div>  
        <p><asp:Literal ID="description" Text='<%# DataBinder.Eval (Container.DataItem, "description") %>' EnableViewState="false" runat="server" /></p>  
    </ItemTemplate> 
    <SeparatorTemplate> 
        <hr /> 
    </SeparatorTemplate> 
</asp:Repeater> 
<asp:Panel ID="ErrorPanel" EnableViewState="false" Visible="false" runat="server">  
    <div class="error">The RSS Feed URL cannot be found!</div> 
</asp:Panel> 
 

 

Create the following code-behind file: ~/UserControls/RssFeed.ascx.cs

using System;  
using System.Data;  
using System.Web;  
using System.Web.UI.WebControls;  
using System.Web.UI.HtmlControls;  
using System.Xml;  
 
public partial class UserControls_RssFeed : System.Web.UI.UserControl  
{  
    private string _RssUrl;  
 
    /// <summary>  
    /// Gets & sets the value of the URL we will use for fetching a RSS 2.0 feed.  
    /// </summary>  
    public string RssUrl  
    {  
        get 
        {  
            return _RssUrl;  
        }  
        set 
        {  
            _RssUrl = value;  
        }  
    }  
 
    protected void Page_Load(object sender, EventArgs e)  
    {  
        if (RssUrl == null || RssUrl == "")  
        {  
            ErrorPanel.Visible = true;  
        }  
        else 
        {  
            FetchRSS();  
        }  
    }  
 
    private void FetchRSS()  
    {  
        // Open an XML reader & fetch the RSS data from the RssUrl.  
        XmlTextReader reader = new XmlTextReader(RssUrl);  
        DataSet ds = new DataSet();  
        ds.ReadXml(reader);  
 
        if (reader == null)  
        {  
            ErrorPanel.Visible = true;  
        }  
        else 
        {  
            RssRepeater.Visible = true;  
            RssRepeater.ItemDataBound += new RepeaterItemEventHandler(RssRepeater_ItemDataBound);  
            RssRepeater.DataSource = ds.Tables[2];  
            RssRepeater.DataBind();  
        }  
    }  
 
    protected void RssRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)  
    {  
        // Execute the following logic for Items and Alternating Items.  
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)  
        {  
            // The RSS 'pubDate' is an optional element.  If we don't have it, don't display it.  
            Literal _pubDate = (Literal)e.Item.FindControl("pubDate");  
            if (_pubDate.Text != "") {  
                _pubDate.Text = FormatDate(_pubDate.Text);  
                _pubDate.Visible = true;  
            }  
        }  
    }  
 
    private string FormatDate(string DateString)  
    {  
        DateTime dt = DateTime.Parse(DateString);  
        return dt.ToString("D");  
    }  
}  
 

 

Step 2: Let Sitefinity know about your new UserControl

In order for Sitefinity to make use of a new UserControl it must declared in the <toolboxControls> section of the "web.config" file.  Add the following line to your "web.config" file:

<toolboxControls>   
    <add name="RSS" section="My Controls" url="~/UserControls/RssFeed.ascx" />    
</toolboxControls>  

 

Step 3: Place your RSS Feed Control onto your Sitefinity page:

When you login to the Sitefinity Admin and alter a page, you should now see a new section called "My Controls" in your Toolbox.  Expand this section and drag & drop your RSS feed control onto your page.  Edit the control and enter the URL for the RSS Feed that you wish to display.


We're done!!  Click here to see it in action!   Now I can capitalize on Ivan's work.  I love the Internet so damn much.

If you would like, you can download this project by clicking here

I will also be making this control available in the GoonDocks Sitefinity Controls.

Facebook DZone It! Digg It! StumbleUpon Technorati Del.icio.us NewsVine Reddit Blinklist Furl it!

Comments  449

Post a comment!


HostMySite.com   website uptime