Create vCalendar items from ASP.NET

Feb 8, 2006

I recently build a web application that would let visitors add calendar items to their calendar program like Outlook or windows mobile smartphone, just by clicking a link and download the vCalendar item. vCalendar is a standard format for calendar items and is used widely across the industry by most calendar programs and tools. It is actually a simple text file, so it is not hard to create them dynamically. This is an example of a method in VB.NET that converts an ASP.NET page into a vCalendar item. Just call the method from the page load or use it in a httphandler.

Private Sub vCalendar(ByVal subject As String, ByVal location As String, ByVal description As String, ByVal dateStart As Date, ByVal dateEnd As Date)
   Dim mStream As New System.IO.MemoryStream
   Dim writer As New System.IO.StreamWriter(mStream, System.Text.Encoding.GetEncoding(1252))

   With writer
      .AutoFlush = True
      .WriteLine("BEGIN:VCALENDAR")
      .WriteLine("PRODID:-//Mads Kristensen//DA")
      .WriteLine("BEGIN:VEVENT")
      .WriteLine("DTSTART:" & dateStart.ToUniversalTime.ToString("yyyyMMdd\THHmmss\Z"))
      .WriteLine("DTEND:" & dateEnd.ToUniversalTime.ToString("yyyyMMdd\THHmmss\Z"))
      .WriteLine("LOCATION:" & location)
      .WriteLine("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" & description)
      .WriteLine("SUMMARY:" & subject)
      .WriteLine("PRIORITY:3")
      .WriteLine("END:VEVENT")
      .WriteLine("END:VCALENDAR")
   End With

   With Response
      .ClearHeaders()
      .AppendHeader("Content-Disposition", "attachment; filename=" & subject & ".vcs")
      .AppendHeader("Content-Length", mStream.Length.ToString())
      .ContentType = "text/calendar"
      .BinaryWrite(mStream.ToArray())
      .End()
   End With
End Sub

See an example vCalender item>

if you are looking for vCard implementation too, here's one.

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (4) -

 James Clark
James Clark
10/23/2006 12:38:06 AM #

Thanks for this article. It is great. I have a question though. Does anyone know how to send a vCalendar item directly from asp.net as an email? I know how to attach a vcs file but I am struggling with how to set the header of the email with a content type of "text/calendar". Thank you in advance for your help.

 Joel Zinn
Joel Zinn
3/25/2007 9:08:57 PM #

Mads.  This is a great, short article.  Thanks for posting it.  
I do have a questions, however.  I am trying to apply a dataset of info that may contain several events that need to be sent to outlook.  I have the vCalendar code set up in a loop and the code should execute once for each row in the table.  For some reason, I am only getting the first one to show up.  The debugger will not take up after the call to your code.  It just seems to stop completely.  Is there a way to process the code more than once in a single response?

Thanks again...

Matt R
Matt R United States
10/15/2007 4:49:22 AM #

I am looking for the same solution as the previous two responders.  Anybody out there know the answer?  I know it's possible, I've visited a friend's intranet who had this set up and I know it was custom code, but there was no way I was going to be able to get the info from this mega-corporation's developers.

Ryan Farley
Ryan Farley United States
8/19/2008 3:23:02 PM #

I just came across this post and thought I would provide a link to a post I wrote recently to the last 3 commenters who wanted to push out multiple events:

ryanfarley.com/.../...a-webpage-via-icalendar.aspx

-Ryan

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.