By , 14 Apr 2010

## Introduction

This article describes how to load and view iCalendars by using the DDay.iCal library. I will cover more advanced topics, such as creating, editing, and serializing iCalendars in my next article.

In this article, I will walk you through creating a console application that will load and display upcoming events to the user. I've also included an example project that demonstrates how to add this kind of support to an ASP.NET web application.

## Background

Many programmers have worked with adding some kind of calendar support to an application - from displaying upcoming events on a web site, to allowing personalized calendars, with the ability to alter them.

So, thousands of programmers, all adding calendar support to their applications. So, what's the problem with that? The historical answer is that no (or very few) programmers followed any kind of standard to implement their calendars. So, if you needed to accomplish anything else with the calendar that your original application didn't support, you'd have to write it by hand.

Also, these ad-hoc calendars are only viewable from the application that wrote them. What if you want to allow for recurrences in your calendar, so an event can recur "every 2nd-to-last Sunday of the month?" What if you want to publish your calendar, so others can subscribe to it, and view it from the calendar program they prefer? What if you want to display and manipulate calendars from multiple sources, including sources that you may not have control over?

These are some of the problems the iCalendar standard solves for us. If you didn't already know -- iCalendar is a W3C recommendation known as RFC 5545. You can find it here.

## Using the Code

To begin, open Visual Studio and create a "Windows Console Application" project. Then, if you haven't already done so, download the latest binary version of DDay.iCal from SourceForge.net. Once you've done that, you simply need to add a reference to DDay.iCal.dll from your project (i.e. click "Add Reference" from the "Project" menu).

Then, add the following to the top of the Program.cs file:

using DDay.iCal;

You're now ready to load your first iCalendar! There are multiple ways you can load iCalendars, ranging from simply loading the file from your local filesystem, to loading from a WebDAV or CalDAV store, to loading from a database. The possibilities are endless; however, in this article, we'll focus on simply loading the file from your local filesystem. Add the following code to your Main() method (of course, replacing the path with the actual path to your iCalendar file).

// Load the calendar file


Congratulations, you've loaded your iCalendar, and are ready to work with it! For now, let's display the events that occur today:

//
// Get all events that occur today.
//
IList&occurrence> occurrences = calendars.GetOccurrences

Console.WriteLine("Today's Events:");

// Iterate through each occurrence and display information about it
foreach (Occurrence occurrence in occurrences)
{
DateTime occurrenceTime = occurrence.Period.StartTime.Local;
IRecurringComponent rc = occurrence.Source as IRecurringComponent;
if (rc != null)
Console.WriteLine(rc.Summary + ": " + occurrenceTime.ToShortTimeString());
}

That's it! To be clear, calendars.GetOccurrences(...) returns a list of Occurrence objects. Occurrence objects describe each occurrence, including the date/time the occurrence happens, and a reference to the original component that generated the occurrence. We cast the Source of the occurrence to an IRecurringComponent, and display its properties.

So, now we've displayed all the events that occur today. Let's display all of the upcoming events that will occur within the next 7 days:

//
// Get all occurrences for the next 7 days, starting tomorrow.
//
occurrences = calendars.GetOccurrences

Console.WriteLine(Environment.NewLine + "Upcoming Events:");

foreach (Occurrence occurrence in occurrences)
{
DateTime occurrenceTime = occurrence.Period.StartTime.Local;
IRecurringComponent rc = occurrence.Source as IRecurringComponent;
if (rc != null)
Console.WriteLine(rc.Summary + ": " + occurrenceTime.ToString());
}

As you can see, this code is nearly identical to "Today's Events", with a slight difference at calendars.GetOccurrences(...). We also display the full date/time of the recurrence this time.

Note: If you're only interested in certain kinds of components (i.e. Events, Todos, etc.), then you can get occurrences for only those events using the generic version of GetOccurrences (i.e. calendars.GetOccurrences<IEvent>(...)).

## Final Code

Here's the final result of Program.cs:

using System;
using System.Collections.Generic;
using System.Text;

// Required DDay.iCal namespace
using DDay.iCal;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

//
// Get all events that occur today.
//
IList&occurrence> occurrences = calendars.GetOccurrences

Console.WriteLine("Today's Events:");

// Iterate through each occurrence and display information about it
foreach (Occurrence occurrence in occurrences)
{
DateTime occurrenceTime = occurrence.Period.StartTime.Local;
IRecurringComponent rc = occurrence.Source as IRecurringComponent;
if (rc != null)
Console.WriteLine(rc.Summary + ": " +
occurrenceTime.ToShortTimeString());
}

//
// Get all occurrences for the next 7 days, starting tomorrow.
//
occurrences = calendars.GetOccurrences

Console.WriteLine(Environment.NewLine + "Upcoming Events:");

foreach (Occurrence occurrence in occurrences)
{
DateTime occurrenceTime = occurrence.Period.StartTime.Local;
IRecurringComponent rc = occurrence.Source as IRecurringComponent;
if (rc != null)
Console.WriteLine(rc.Summary + ": " + occurrenceTime.ToString());
}
}
}
}

## Points of Interest

For more information, visit the DDay.iCal homepage at ddaysoftware.com.

Many apologies that it's taken so long to update this article. I'll now be providing more frequent updates.

## History

• 04/14/2010 - Updated to version 1.0 Alpha
• 03/09/2007 - Posted

 Douglas Day Web Developer United States Member
Doug has been a Software Engineer for 7 of the previous 9 years, and has 12 years of programming experience.

For the past 3 years, he has been developing custom applications in C# and Visual Basic.NET, with an emphasis on custom cross-Internet applications for IT management, real-time collaboration, and process management and reporting.

Votes of 3 or less require a comment

 Search this forum Profile popups    Spacing RelaxedCompactTight   Noise Very HighHighMediumLowVery Low   Layout Open AllThread ViewNo JavascriptPreview   Per page 102550
 First PrevNext
 Help for newbie with errors line 20 ptsmh 10 Nov '11 - 9:49
 Hello thanks for great article.   I am trying to learn C# and write a simple program to read in iCal files from either local computer or internet and your article appears to be very helpful.   However when I copy entire code to C# and start debugging I get compile errors.   Error 1 Using the generic type 'System.Collections.Generic.IList' requires 1 type arguments C:\TestiCAL\iCalTester\iCalTester\Program.cs   Error 2 The name 'occurrence' does not exist in the current context C:\TestiCAL\iCalTester\iCalTester\Program.cs and there are another 4 similar errors for lines 20, 26,38 and 44.   Any help would be appreciated thanks. Sign In·View Thread·Permalink
 Sample or guidance on creating / reading VFREEBUSY rw_architect 9 Sep '10 - 13:22
 Hello again Doug,   I've been studying your code a bit further and I'm a little confused about the FreeBusy implementation. I can't sort it out .   It looks like some VFREEBUSY constructs have been commented out, notably in the DDAY.iCal.ComponentFactory Build routine. It also looks like VFREEBUSY is implemented by way of IUniqueComponent... but after that I'm lost in my confusion.   How is FreeBusy implemented? How do I build a VFREEBUSY section of iCal? Is there a sample I can download or guidance I can read elsewhere?   Again, thanks much for your help.   - Randolph Sign In·View Thread·Permalink
 Re: Sample or guidance on creating / reading VFREEBUSY Douglas Day 10 Sep '10 - 4:30
 Randolph,   VFREEBUSY hasn't been fully implemented, yet. As the project nears a full version, I plan on implementing it. I've got so many other things on my plate, I can't tackle it immediately. If you'd like to contribute to add functionality for VFREEBUSY, I'd love any help.   Thanks, -Doug Sign In·View Thread·Permalink
 Re: Sample or guidance on creating / reading VFREEBUSY rw_architect 16 Oct '10 - 5:19
 Hello Doug,   I completely understand. I too am short of time right now. I do need to implement VFREEBUSY as part of a larger solution I'm working on and have been studying up on it's structure. When I complete the implementation, I will gladly send you the code. Thanks for your help and for sharing this library.   - Randolph Sign In·View Thread·Permalink
 Re: Sample or guidance on creating / reading VFREEBUSY Douglas Day 26 Oct '10 - 10:33
 Randolph,   I found a little time, and actually implemented basic Free/Busy support. To get the code, you'll need to download the trunk code from Subversion. You can find instructions on Sourceforge to do that. Once you have the most recent code, you'll want to look at the FreeBusyTest.cs class in the DDay.iCal.Test project to see an example of how it's used.   I think the functionality of DDay.iCal is getting good enough I need to create a few more examples of how to use these features, since I think they'll be fairly popular.   Thanks! -Doug Sign In·View Thread·Permalink
 VB.Net Example1 variable declaration errors... rw_architect 5 Sep '10 - 17:07
 Hello Doug,   Thanks for posting this article and code. It's helping me to better understand (and hopefully use) the iCal format.   In the VB.Net Example1 solution included in the download, the following lines in the btnMakeICS_Click event are showing as errors.   (1) Dim MyEvent As DDay.iCal.Components.Event = MyCal.Create(Of DDay.iCal.Components.Event)()   and directly following this line in the 'With MyEvent' block   (2) .Start = New DDay.iCal.DataTypes.iCalDateTime(DirectCast(Item("Start"), Date)) (3) .End = New DDay.iCal.DataTypes.iCalDateTime(DirectCast(Item("End"), Date)) (4) .Summary = New DDay.iCal.DataTypes.Text(DirectCast(Item("Summary"), String)) (5) .Description = New DDay.iCal.DataTypes.Text(DirectCast(Item("Description"), String))   I assumed the problem was namespace related and after looking through the Object Browser I think I corrected the first 3 errors by removing the 'COMPONENTS' namespace from the type declaration. For the last 2 lines, however, I could not find the 'TEXT' type or the 'DATATYPES' namespace anywhere in the object browser. Did I miss it? Can you update the sample or tell me how to correct this? Thanks,   - Randolph Sign In·View Thread·Permalink
 Re: VB.Net Example1 variable declaration errors... Douglas Day 7 Sep '10 - 5:09
 Randolph,   I forgot to update the VB example for version 1.0. It's now updated in the trunk of Subversion, and will be included with the next release of DDay.iCal.   Thanks for pointing that out   Regards, -Doug Sign In·View Thread·Permalink
 Re: VB.Net Example1 variable declaration errors... Douglas Day 7 Sep '10 - 5:18
 FYI,   If you'd like to fix the errors yourself, here's what you do.   - The DDay.iCal.Components namespace was removed. DDay.iCal.Components.Event is now DDay.iCal.Event. - The DDay.iCal.DataTypes namespace was removed. DDay.iCal.DataTypes.iCalDateTime is now DDay.iCal.iCalDateTime. - The 'Text' class no longer exists. .Summary and .Description now accept a normal string value.   Regards, -Doug Sign In·View Thread·Permalink
 Re: VB.Net Example1 variable declaration errors... rw_architect 9 Sep '10 - 12:55
 Hello Doug.   Thanks for the reply. I applied the changes you pointed out and they work fine. I left out of my original message one offending line in that vb sample... the line that creates a serializer for the specified calendar object.   Dim MySerializer As New DDay.iCal.Serialization.iCalendarSerializer(MyCal)   This line errors. Thanks again for your help.   - Randolph Sign In·View Thread·Permalink
 Re: VB.Net Example1 variable declaration errors... Douglas Day 9 Sep '10 - 13:00
 Would be nice if article could be updated for version 0.80 manudurand 14 Mar '10 - 1:52
 Hi Doug, First of all, thanks a lot for this project, it's really useful. I intend to use your library to consume .ics calendars (published to web from Outlook 2007) and display the events on a Calendar control on an aspx page, all in C# code-behind. It would be nice if you could update this article (or create a new one) with the newest version on DDay.iCal (version 0.80). In particular, could you describe what is the Occurence object relative to the Event object? Which one should I loop through to get my list of events on each day (when my DayRenderEventHandler gets fired)? Thanks again, Emmanuel Sign In·View Thread·Permalink
 Funny you mention an updated article. I'm currently making some major changes to DDay.iCal and I'll update this article when that new version is released (likely to be version 0.90).   What you want to do is call Event.GetOccurrences(...) and use the list of Occurrence objects returned in your DayRenderEventHandler to show the different events.   Let me know if you have more questions.   Thanks, -Doug Sign In·View Thread·Permalink
 Re: Would be nice if article could be updated for version 0.80 manudurand 14 Mar '10 - 16:21
 Hi Doug, Right now, I'm calling _Calendars.GetOccurrences(...) which returns me a list of Event Occurences. Then, for each day, I loop through each Occurrence object from the list. Which date should I compare with my DayRender date? occ.Period.StartTime.Local.Date or occ.Component.Start.Local.Date? Not quite sure what's the difference between these two... Thanks again, Emmanuel Sign In·View Thread·Permalink
 Use occ.Period.StartTime.Local.Date. This represents the current occurrence date/time, whereas occ.Component.Start.Local.Date represents the date/time of the original component. For example, if you schedule an event that starts on March 14th, 2010 at 8:00 AM, and recurs every week on the same day/time. evt.GetOccurrences(...) would return the following dates (contained in Occurrence.Period):   3/14/2010 8:00 AM 3/21/2010 8:00 AM 3/28/2010 8:00 AM 4/4/2010 8:00 AM etc.   You'll notice on each of these Occurrence objects, the 'Component' points back to the original event (March 14th, 2010 at 8:00, and recurs every week).   Hope that clears it up a bit.   Let me know if you have more questions.   Thanks, -Doug Sign In·View Thread·Permalink
 Re: Would be nice if article could be updated for version 0.80 manudurand 14 Mar '10 - 19:50
 Got it now. Thanks for the explanation, it's very much appreciated. I'll keep an eye on the next version release (any expected date?). Emmanuel Sign In·View Thread·Permalink
 Glad it's working   I plan on releasing the new version within the next week, two at most. It's been an almost complete rewrite of the code base to allow for better parsing, serialization, and object-model simplicity.   Keep on eye on SourceForge for the release.   Regards, -Doug Sign In·View Thread·Permalink
 great article jael36 24 Nov '09 - 9:55
 thanks for this. I have been looking into something like this all day...   my big question is: can ics files support outlook tasks as well as outlook calendar appointments?   The reason i ask is that everyone is concerned about synchronizing with the outlook calendar, but not many about setting new tasks!   Also, can we limit this to a 1-way feed? From a custom web calendar to outlook but not the reverse? I suppose it would be a setting within the custom web calendar. Sign In·View Thread·Permalink
 Re: great article Douglas Day 29 Dec '09 - 10:01
 Yes, iCalendar support Events as well as Todo items.   As far as the 1-way feed, that would be a setting in Outlook, or in the web server that distributed the iCalendar.   And sorry about the late response, just saw this -Doug Sign In·View Thread·Permalink
 Thanks Member 4557583 5 Aug '09 - 3:23
 Hi Doug,   Thank you for such a wonderful post. It helped me a lot. Can you also give me an idea how to parse vcards.   Thanks Panna Sign In·View Thread·Permalink
 Re: Thanks Douglas Day 5 Aug '09 - 4:12
 Panna,   I'm glad you've found DDay.iCal useful - don't forget to use the most recent version (now 0.71) from SourceForge.net[^], if you haven't done so already.   As far as vcards go, I haven't really given it much thought. In theory, it's a very close format to iCalendar, so I could share some of the code base and create a library for it. I'll give some thought to it, and if I have the time and use for it, I'll put it together.   In the meantime, there are several articles on CodeProject about vcard. Just search for "vcard c#".   -Doug Sign In·View Thread·Permalink
 Event.OccursOn and events that span two UTC days but is only 2 hours long i.i. wiin 30 Apr '09 - 14:38
 I'm having difficulties getting the Event.OccursOn to work properly. I have an event that starts at the 17th at 4PM Mountain Standard Time and runs for 2 hours. This converts to 11PM UTC to 1AM UTC the next day. Event.OccursOn evals to true irregardless of which of the two days (the 17th or 18th) I pass in as a UTC date. So when iterating thru the dates (as in the console app), that same event is now listed for both days, but the date is displayed using "evt.Start.Local.ToShortTimeString()". This is very confusing because is shows a two hour event starting at 4PM the 17th and 4PM the 18th as well. It should only display for the 17th. I'm having to manually check each Start Date and End Date with my own logic to really tell if that event is for the 17th or the 18th. Or am I confused here? Sign In·View Thread·Permalink