Coldfusion Tutorials



Spry Tutorials

Air Tutorials


Creating RSS Feeds

With the release of Coldfusion 8 you are now able to both read and write RSS or Atom feeds using a new tag called CFFEED. This tutorial will cover how to create an RSS feed.

An RSS feed is really nothing more than an specially formatted XML document, as such the CFFEED tag enables you to easily read and write these specially formatted XML documents. To create a RSS feed there are a few things that are required in order for your RSS feed to be valid, and also work with the CFFEED tag. The CFFEED tag does not require you to do anything when creating an Atom feed so you are responsible for creating a valid feed (so be careful!)

  1. Feed Title, Link, Description, version
  2. At least one item element must be created
Other than the above requirements what other information you include in your feed is optional.

Creating the Feed

The code below details how to create an RSS feed with a single item. In most cases you will want to create your feed items dynamically using the output from a query.
<cfscript>
myStruct = StructNew();
mystruct.link = "http://www.garyrgilbert.com/tutorials/coldfusion/advanced/";
myStruct.title = "Gary Gilbert's Advanced Coldfusion Tutorials";
mystruct.description = "Coldfusion Tutorials from beginner to Advanced";
mystruct.pubDate = Now();
mystruct.version = "rss_2.0";

myStruct.item = ArrayNew(1);
myStruct.item[1] = StructNew();
myStruct.item[1].description = StructNew();
myStruct.item[1].description.value = "How to create an RSS feed with Coldfusion 8's CFFEED Tag";
myStruct.item[1].link = "http://www.garyrgilbert.com/tutorials/coldfusion/advanced/CreateRSS.cfm";
myStruct.item[1].pubDate = Now();
myStruct.item[1].title = "Creating RSS Feeds";
</cfscript>
<cffeed action = "create"
name = "#myStruct#"
outputFile = "AdvancedTutorialsFeed.xml"
overwrite = "yes"
xmlVar = "myXML">


Creating a Feed from a Query

To create an RSS feed from a query it is not all that much different than you would create the feed normally. The problem is of course that most of the time your query columns are not going to be named exactly the same as what the RSS feed names should be. In this case Coldfusion provides you with a mechanism to map your query columns to the appropriate feed attributes.
Just like in the code example above we need to set up our feed meta data manually in a structure. Once we have done that we create a query to retrieve our feed items and then map the columns of our query to the feed attributes.
<cfquery name="getAdvancedCf" datasource="dbTutorials">
SELECT * FROM tutorials
where level="Advanced"
and tech="cf"
</cfquery>

<cfscript>
myStruct = StructNew();
mystruct.link = "http://www.garyrgilbert.com/tutorials/coldfusion/advanced/index.cfm?tutorial=";
myStruct.title = "Gary Gilbert's Advanced Coldfusion Tutorials";
mystruct.description = "Coldfusion Tutorials from beginner to Advanced";
mystruct.pubDate = Now();
mystruct.version = "rss_2.0";

// Map the orders column names to the feed query column names.
columnMapStruct = StructNew();
columnMapStruct.publisheddate = "createDate";
columnMapStruct.content = "body";
columnMapStruct.title = "title";
columnMapStruct.rsslink = "ID";
</cfscript>
<cffeed action = "create"
query= "#getAdvancedCf#"
properties="#mystruct#"
columnMap="#columnMapStruct#"
outputFile = "AdvancedTutorialsFeed.xml"
overwrite = "yes"
xmlVar = "myXML">


Conclusion

As you can see from the above example code setting up an RSS feed using Coldfusion is really quite simple. There are some additional RSS properties that you can add to your RSS feed and I encourage you to check out the documentation on RSS feeds at http://livedocs.adobe.com/coldfusion/8/htmldocs/Tags_f_01.html#4002452