RSS is rapidly becoming a popular format for providing content. It allows users to subscribe to bits of information, so they can be notified when new information has been posted.
I’ve been tossing around the idea of implementing RSS at work lately, and wanted to do it with ColdFusion. I finally buckled down and put something together. Surprisingly, it was much easier to do that I thought it would be, and I ended up creating RSS feeds for news articles, upcoming events, and job opportunities.
Here’s the process I used to create the RSS feed for job opportunities.
The first thing in my CFM file is a block of code that makes sure no HTML outside of the cfoutput tag gets displayed:
<cfsetting enablecfoutputonly="yes">
Then the query:
<cfquery name="qCareers" datasource="xxx">
SELECT * FROM Careers
ORDER BY Position
</cfquery>
Then I implement a cfsavecontent tag. This makes sure that all the content between these two tags is saved as a variable. The closing tag appears later.
<cfsavecontent variable="theXML">
Then comes my output. It is important than the root tag in XML for an RSS feed is “channel”, that the title of the feed is in a “title” tag, feed description is in “description”, a link to the HTML content of the feed is in a “link” tag. As well, every item should be within an “item” tag, which also contains “title”, “description” and “link” tags. Not following that format will cause your RSS feed to fail.
In addition, there must be an “rss” tag, and there must be no white space (including a new line) between the opening “cfoutput” tag and the “xml” tag.
<cfoutput><?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
<channel>
<title>Faculty of Management - Jobs</title>
<description>Current Career Opportunities</description>
<link>http://www.uleth.ca/man/people/jobs/</link>
<cfloop query="qCareers">
<item>
<title>#Position#</title>
<description>#Details# <cfif NOT IsDefined("StartDate") OR StartDate DOES NOT CONTAIN "1999">Commences #DateFormat(StartDate,'dd mmmm yyyy')#.</cfif></description>
<link>http://www.uleth.ca/man/jobs/index.cfm?id=#RecordID#</link>
</item>
</cfloop>
</channel>
Close the cfsavecontent tag, as previously mentioned.
</cfsavecontent>
Then output the variable that contains it all.
<cfcontent type="text/xml">
<cfoutput>#theXml#</cfoutput>
Simply upload this file to your server somewhere, and add the following into the page where you want the RSS feed available.
<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="http://www.uleth.ca/man/rss/jobs.cfm" />
Simple as that.
You have NOOOOOO idea how helpfull this article was. I was going CrAzY with this damn RSS feed. I am new to CF from an HTML background. Thanks for the info.
I’m glad you found it useful, Josh.
Thanks so much! I was worried that ColdFusion 5.0 couldn’t handle an RSS feed (other tutorials used cfxml which isn’t featured in 5.0) but it turned out to be no problem in the end.
I’m glad it worked for you.
THANKS !!!
This is really helpful.
One problem, what do you do if your data contains & and ‘ that the RSS does not like. How do I change these?
That’s something you’ll need to take care of in your database settings.