Create your first Rss Feed Burner in umbraco

How create RSS Feed consumer in umbraco

In umbraco cms development process how we will create RSS Feed which will take url of any other website and show there latest updated content. In this post I have describe how we will crete RSS Feed burner for umbraco based application.

Here I am achieving feed consuming functionality by .Net user control. For simplicity I have shared here code you just need to copy and pest and it will work as you want.

First Create User control called Feed Burner 


using System;
using System.Data;
using System.Xml;
using System.Net;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.XPath;
using System.ServiceModel.Syndication;
using System.Collections.Generic;

namespace FeedBurner.usercontrols
{
public partial class ViewFeedBurner : System.Web.UI.UserControl
{
#region Declare Private Properties

// Declare Feed Url from where data will be retrive.
private string _feedUrl;
public string FeedUrl
{
get { return _feedUrl; }
set
{
// Check for valid URL
//if (FeedUrl.Length != null)
// {
_feedUrl = value;
// }
// else
// {
// Set Invalid Url Error Message.
// }
}
}

#endregion

#region Event Handler

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DisplayFeed();
}
}

#endregion

#region User Define Method

public void DisplayFeed()
{
try
{
// Read the feed using an XmlReader
XmlReader reader = XmlReader.Create(FeedUrl);
SyndicationFeed feed = SyndicationFeed.Load(reader);

//Bind Data In list view
lstViewNewsFeeds.DataSource = feed.Items;
lstViewNewsFeeds.DataBind();
}
catch (Exception ex)
{
if (ex is WebException || ex is XmlException)
{
// Handle bad url, timeout or xml error here.
}
else
throw;
}
}

/// <summary>
/// Url Validation
/// </summary>
/// <param name="feedUrl">Feed Url</param>
/// <returns>bool</returns>
public bool IsValidUri(string feedUrl)
{
Uri validatedUri;
return Uri.TryCreate(feedUrl, UriKind.RelativeOrAbsolute, out validatedUri);
}

#endregion

/// <summary>
/// Bound List view data item.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lstViewNewsFeeds_ItemDataBound(object sender, ListViewItemEventArgs e)
{
try
{
// Instantiate listview control object.
ListViewDataItem lvDataItem = (ListViewDataItem)e.Item;

//Make sure that data item is binding with listview.
if (lvDataItem.ItemType == ListViewItemType.DataItem)
{

SyndicationItem item = e.Item.DataItem as SyndicationItem;

Panel panelNewsDescContent = (Panel)e.Item.FindControl("panelNewsDescription");
HyperLink uriTitleLink = (HyperLink)e.Item.FindControl("hlNewsTitle");

if (uriTitleLink != null)
{
uriTitleLink.Text = Convert.ToString(item.Title.Text);
uriTitleLink.NavigateUrl = item.Links[0].Uri.AbsoluteUri;
}
// Make sure that panel object is referenced.
if (panelNewsDescContent != null)
{
Label lblBox = new Label();
lblBox.Text = Convert.ToString(item.Summary.Text);
panelNewsDescContent.Controls.Add(lblBox);
}
}
}
catch(Exception ee)
{
Response.Write(ee.Message);
}
}
}
}

After Creating code behind file create ascx page.


<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ViewFeedBurner.ascx.cs" Inherits="FeedBurner.usercontrols.ViewFeedBurner" %>

<div id="divMainBar">
<%-- Begin List View --%>
<asp:ListView ID="lstViewNewsFeeds" runat="server" OnItemDataBound="lstViewNewsFeeds_ItemDataBound">
<%-- Bigin: LAYOUT TEMPLATE --%>
<LayoutTemplate>
<div runat="server" id="ItemPlaceHolder" style="width:100%"></div>
<div></div>
</LayoutTemplate>
<%-- End: LAYOUT TEMPLATE --%>

<%-- Bigin: ITEM TEMPLATE --%>
<ItemTemplate>
<%-- Start of the main container --%>
<asp:Panel ID="pnlHeader" runat="server" CssClass="pnlNewsContentPanel">
<asp:HyperLink ID="hlNewsTitle" runat="server"></asp:HyperLink>
<asp:Panel ID="panelNewsDescription" runat="server" CssClass = "pnlNewsDescription"></asp:Panel>
</asp:Panel>
<%-- End of the main container --%>
</ItemTemplate>
<%-- End: ITEM TEMPLATE --%>

<%-- Bigin: EMPTY LAYOUT TEMPLATE --%>
<EmptyDataTemplate>
<div>
Sorry, there are no items to display.
</div>
</EmptyDataTemplate>
<%-- End: EMPTY LAYOUT TEMPLATE --%>
</asp:ListView>
<%-- End List View --%>
</div>

After Creating the ascx page compile and Use it as macro in umbraco page and pass FeedUrl property to link like: {Rss Link of any website like yahoo, google and any thing you want.}

Another Umbraco Article

If you find any problem feel free to leave comment.

Advertisements

2 thoughts on “Create your first Rss Feed Burner in umbraco

  1. Pingback: Umbraco: How to Create Custom data type in umbraco using c#. | [Code Destination]

  2. Pingback: Install umbraco manually. | [Code Destination]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s