Thursday, August 2, 2012

Return of the Cheese Syndicate–Creating an RSS 2.0 feed in ASP.Net 4.0

Hack the Gibson

In a prior post (HERE) I described how to consume an RSS Feed in ASP.Net 4.0.  Let’s take a look at how to create one.

We’re going to need to reference the System.ServiceModel.Syndication namespace in our application.  The following properties are available to a SyndicationItem -

Name

Description

AttributeExtensions Gets the attribute extensions for the syndication item.
Authors Gets the authors of the syndication item.
BaseUri Gets and sets the base Uniform Resource Identifier (URI) for the SyndicationItem instance.
Categories Gets the syndication categories for the syndication item.
Content Gets and sets the content of the syndication item.
Contributors Gets the contributors of the syndication item.
Copyright Gets and sets the copyright information for the syndication item.
ElementExtensions Gets the element extensions contained in the syndication item.
ID Gets and sets the ID of the syndication item.
LastUpdatedTime Gets and sets the last updated time for the syndication item.
Links Gets the links contained in the syndication item.
PublishDate Gets and sets the publish date for the syndication item.
SourceFeed Gets and sets the source feed of the syndication item.
Summary Gets and sets the summary of the syndication item.
Title Gets and sets the title of the syndication item.

We will populate some of these properties to create our RSS Feed. 

To start, we will need to create an empty website (This article assumes you know how to create an empty ASP.Net Website).
Add a aspx page (This article assumes you know how to add a new Web Form).
In my example, the name ended up being Default.aspx (which is how I will refer to the form).
image

Now we will need to go to the code behind and work with the Load Event/Trigger.
image

This in turn gives us the pre-formatted event:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) _
Handles Me.Load

End Sub




Which is where the magic will happen.  So let’s look at what we need to do from here.  First we need to import our namespace.  Right-Click your solution name in the Solution Explorer | Add Reference.  Make sure to move to the .NET table and it is selected.    Then choose System.ServiceModel -

image



Click OK.



Now back on our code behind for Default.aspx.vb we need to Import the namespace in the declarations -



Imports System.ServiceModel.Syndication




We will need to make a connectionString in our web.config like so -



<configuration>
<
connectionStrings>
<
add name="dataconnect"
connectionString="Server=SERVERNAME;Database=Blog;
Trusted_Connection=True;
" providerName="System.Data.SqlClient"/>
</
connectionStrings>




Obviously, you will want to change your Server and Database parameters to the ones appropriate for your instance.



Now that we have the connection, we need to create the class that we will use to fetch the data from our database (In order to simplify the article, I will not be using Tiers and understand that this is against best practices).



Right-Click the solution name in the Solution Explorer | Add New Item | Class.  Name your class BlogRoll.vb | Click OK.



We need to import some namespaces for references in our declarations -



Imports System.Web.Configuration.WebConfigurationManager
Imports System.Data.SqlClient




Now our setup is complete, we can move onto the coding portion.

First we need to create the properties of our BlogRoll object -




Public Property blog_cd As Integer
Public Property
blog_dt As DateTime
Public Property blog_summary As String
Public Property
blog_content As String
Public Property
blog_title As String
Public Property
blog_author As String
Public Property
blog_url As String




Now we are going to create our polymorph Sub New() -



    Public Sub New()
Me.blog_cd = 0
Me.blog_dt = DateTime.MinValue
Me.blog_summary = String.Empty
Me.blog_content = String.Empty
Me.blog_title = String.Empty
Me.blog_author = String.Empty
Me.blog_url = String.Empty
End Sub
Public Sub New
(ByVal cd As Integer, _
ByVal dt As DateTime, _
ByVal summary As String, _
ByVal content As String, _
ByVal title As String, _
ByVal author As String, _
ByVal url As String)
Me.blog_cd = cd
Me.blog_dt = dt
Me.blog_summary = summary
Me.blog_content = content
Me.blog_title = title
Me.blog_author = author
Me.blog_url = url
End Sub




Now that we have our polymorph Sub New() that will construct our properties, we can move on to populating our list of BlogRoll.  First we need to create a Function that will fetch the data from the database using a SQLDataReader.  We will then convert that reader to a List of BlogRoll.  Finally, we’ll return our list to the calling method (default.aspx.vb\Page_Load).



First thing is let’s get our values -

(This method calls a Stored Procedure called dbo.usp_GetBlogRoll)




Public Function getBlogRoll() As List(Of BlogRoll)
Dim cn As SqlConnection = New _
SqlConnection(ConnectionStrings("dataconnect").ToString())
Dim cmd As New SqlCommand("dbo.usp_GetBlogRoll", cn)
cmd.CommandType = System.Data.CommandType.StoredProcedure
cn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
Dim blogroll As List(Of BlogRoll) = ConvertReader(reader)
cn.Close()
Return blogroll
End Function




If you’ll notice, we have a method called ConvertReader, let’s tackle that guy next -



Public Function ConvertReader(ByVal reader As SqlDataReader) _
As List(Of BlogRoll)
Dim blog As List(Of BlogRoll) = New List(Of BlogRoll)
If reader.HasRows() Then
While
reader.Read()
blog.Add(New BlogRoll(CInt(reader.Item("blog_cd")), _
reader.Item("blog_dt"), _
reader.Item("blog_summary").ToString(), _
reader.Item("blog_content").ToString(), _
reader.Item("blog_title").ToString(), _
reader.Item("blog_author").ToString(), _
reader.Item("blog_url").ToString()))

End While
End If
Return
blog
End Function




This gets our blog from the database.  Back to the Stored Procedure -



Use [Blog]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE
dbo.usp_GetBlogRoll
AS
BEGIN
SET NOCOUNT ON
;
SELECT
TOP
5
blog_cd
, blog_dt
, blog_summary
, blog_content
, blog_title
, blog_author
, blog_url
From dbo.t_blog_roll
END
GO




This gets the Top 5 (lastest 5) blogs from the table -



USE [Blog]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE
[dbo].[t_blog_roll](
[blog_cd] [int] IDENTITY(1,1) NOT NULL,
[blog_dt] [datetime] NULL,
[blog_summary] [varchar](250) NULL,
[blog_content] [varchar](max) NULL,
[blog_title] [varchar](150) NULL,
[blog_author] [varchar](50) NULL,
[blog_url] [varchar](250) NULL
)
ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO



We’re almost done.  We need to go back to our code behind in Default.aspx.vb page to finish up.  We need to add the following code to populate the page -




Protected Sub Page_Load(sender As Object, e As System.EventArgs) _
Handles Me.Load
Dim blogUri As Uri = New Uri("http://www.urltoyouractualblog.com")
Dim blogName As String = "This is the name of my blog"
Dim blogDescription As String = "Description of my blog"
Dim blogObject As BlogRoll = New BlogRoll
Dim blogItems As List(Of SyndicationItem) = New _
List(Of SyndicationItem)
Dim blogRoll As List(Of BlogRoll) = blogObject.getBlogRoll()
For Each blog As BlogRoll In blogRoll
Dim blogItem As SyndicationItem = New SyndicationItem
blogItem.Title = New TextSyndicationContent(blog.blog_title)
blogItem.Summary = New TextSyndicationContent(blog.blog_summary)
'... Other properties populated here...
blogItems.Add(blogItem)
Next
Dim
feed As SyndicationFeed = New SyndicationFeed(blogName, _
blogDescription, blogUri)
feed.Items = blogItems
Response.Clear()
Response.ContentType = "application/rss+xml"
Dim format As Rss20FeedFormatter = New Rss20FeedFormatter(feed)
Dim blogWriter As System.Xml.XmlWriter = _
System.Xml.XmlWriter.Create(Response.Output, Nothing)
format.WriteTo(blogWriter)
blogWriter.Flush()
Response.End()
End Sub




Once we run this, we the form -

image



 



 



Link to how to consume a RSS 2.0 feed in ASP.Net 4.0:

http://chadcarter30101.blogspot.com/2012/05/secrets-of-cheese-syndicate-consuming.html



Happy .Netting…

Hack the Gibson

3 comments:

  1. Every successful organization is backed by a highly-potential team of employees who work together by combining their skills, capabilities and experience to produce the best solution.

    .Net Development

    ReplyDelete
  2. software development in .NET ASP platform that is provides by Microsoft for developers. In which all types of applications have designed, those are more secured and easy to understand. In Windows 8 .NET have used.

    ReplyDelete