Generating an iCalendar .ics file using ColdFusion

I've been working on a small project at work to allow a user to click a link from an event registration confirmation email, which will open a .ics file in the browser. This special file is in the iCalendar format, and is recognized by MS Outlook, Mozilla Sunbird, and other calendaring tools. It creates an entry in your calendar, with a 30 minute reminder alert, to help you remember to attend the webinar event you registered for. I learned from the vCal() function on cflib.org, and will be submitting my new iCalUS() UDF soon. But here is the code now, as it takes some time to get verified for inclusion on the CFLib.org site.

Download the code in a zip file.

I wrote the function to work in the U.S. and account for daylight savings time. Hopefully developers outside the U.S. can adapt this code to fit their timezones accordingly. Here is the code used for the test submission form and cfcontent/cfheader tags for the following demo.

Change any field(s) below and submit to generate a calendar file: <b>newAppointment.ics</b>
<P>
<cfoutput>
<form method="post">
<table>
<tr>
   <td align="right">Organizer name</td>
   <td><input type="Text" name="on" value="#Form.on#" size="30"></td>
</tr>
<tr>
   <td align="right">Organizer email</td>
   <td><input type="Text" name="oe" value="#Form.oe#" size="30"></td>
</tr>
<tr>
   <td align="right">Description</td>
   <td><input type="Text" name="desc" value="#Form.desc#" size="60"> (use \n sequences for newlines)</td>
</tr>
<tr>
   <td align="right">Subject</td>
   <td><input type="Text" name="sub" value="#Form.sub#" size="30"></td>
</tr>
<tr>
   <td align="right">Location</td>
   <td><input type="Text" name="loc" value="#Form.loc#" size="30"></td>
</tr>
<tr>
   <td align="right">Start Date/Time</td>
   <td><input type="Text" name="st" value="#Form.st#" size="20"> (format: <b>m/d/yyyy HH:mm</b> OR <b>h:mm TT</b> -- this is Eastern time)</td>
</tr>
<tr>
   <td align="right">End Date/Time</td>
   <td><input type="Text" name="et" value="#Form.et#" size="20"> (format: <b>m/d/yyyy HH:mm</b> OR <b>h:mm TT</b> -- this is Eastern time)</td>
</tr>
</table>
<input type="Submit" name="Submit" value="Submit">
</form>
</cfoutput>

<cfif IsDefined("Form.Submit")>
   <cfset eventStr = StructNew()>
   <cfset eventStr.organizerName = Form.on>
   <cfset eventStr.organizerEmail = Form.oe>   
   <cfset eventStr.startTime = ParseDateTime(Form.st)>
   <cfset eventStr.endTime = ParseDateTime(Form.et)>
   <cfset eventStr.subject = Form.sub>
   <cfset eventStr.location = Form.loc>
   <cfset eventStr.description = Form.desc>
   <cfcontent type="text/calendar" reset="Yes">
   <cfheader name="Content-Disposition" value="inline; filename=newAppointment.ics"><cfoutput>#iCalUS(eventStr)#</cfoutput>
</cfif>

Here is a demo of this in action.

-- Update 4/10/08: I submitted the UDF to cflib.org today. Hopefully Ray will post it soon. --

Comments
Michael Santoroski's Gravatar This looks great, but there seems to be some weird characters in your code, and I can't find it on cflib.org. Can you send me the cfc? Thanks!
# Posted by Michael Santoroski | 4/9/08 4:00 PM
Troy's Gravatar It's in the queue for cflib.org inclusion. The last function I submitted took so long I had to ping Ray to finally get it posted. Might need to do that again, but I just submitted to that site last week on 4/10.
# Posted by Troy | 4/15/08 3:38 PM
Paul Riker's Gravatar Sweet work! Thanks!

Go Whitesox!!
# Posted by Paul Riker | 6/27/08 4:50 PM
Jason Dean's Gravatar Awesome post. Thanks Troy. This will be useful.
# Posted by Jason Dean | 7/8/08 2:09 PM
john's Gravatar hi,
thanks for this great post. just curious, how can we add additional fields so that we can add some event details? we want to integrate into outlook
# Posted by john | 7/9/08 9:19 AM
haylo75's Gravatar I just checked cflib.org and couldn't find this UDF on there :( Looks good though, I may have an application for something similar.
# Posted by haylo75 | 7/22/08 1:23 PM
Jim Priest's Gravatar Any chance I could get a copy of this? 4 months later and I can't find this on CFLib...
# Posted by Jim Priest | 8/10/08 2:50 PM
Jason Yin's Gravatar Hi
I was trying to put your code to our website. when I run it, I can create the iCal file, but when I open it, I got a error to import ti the outlook.
The error message: This error can appear if you have attempted to save a recurring Lunar appointment in iCalendar format.
To avoid this error, set the appointment option to Gregorian instead of Lunar.
here is my test website:
http://1bosdev1.conferon.com/demo/demo2007/Agenda/...
Please help me. We would like to use your code in our application.

Thanks
Jason
# Posted by Jason Yin | 9/17/08 10:53 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. Contact Blog Owner