Click here to Skip to main content
11,641,209 members (63,129 online)
Click here to Skip to main content

Adding iCalendar Support to Your Program - Part 1

, 14 Apr 2010 BSD 354.2K 2.2K 123
Rate this:
Please Sign up or sign in to vote.
This article describes the steps to load and view iCalendars in your program by using the DDay.iCal library
Screenshot - iCalendar_Part1.jpg

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
IICalendarCollection calendars = iCalendar.LoadFromFile(@"path\to\your\icalendar.ics");

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
	(DateTime.Today, DateTime.Today.AddDays(1));

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
		(DateTime.Today.AddDays(1), DateTime.Today.AddDays(7));

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

// Start with tomorrow
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)
        {
            // Load the calendar file
            IICalendarCollection calendars = iCalendar.LoadFromFile(@"Business.ics");

            //
            // Get all events that occur today.
            //
            IList&occurrence> occurrences = calendars.GetOccurrences
				(DateTime.Today, DateTime.Today.AddDays(1));

            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
		(DateTime.Today.AddDays(1), DateTime.Today.AddDays(7));

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

            // Start with tomorrow
            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

License

This article, along with any associated source code and files, is licensed under The BSD License

Share

About the Author

Douglas Day
Web Developer
United States United States
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.

You may also be interested in...

Comments and Discussions

 
QuestionICalendar Support for Visual Basic Pin
Member 1145177814-Feb-15 0:18
memberMember 1145177814-Feb-15 0:18 
Questiondoes this work with VS2012 and windows phone 8? Pin
kunwar_9124-Sep-13 8:39
memberkunwar_9124-Sep-13 8:39 
QuestionHelp for newbie with errors line 20 Pin
ptsmh10-Nov-11 9:49
memberptsmh10-Nov-11 9:49 
GeneralSample or guidance on creating / reading VFREEBUSY Pin
rw_architect9-Sep-10 13:22
memberrw_architect9-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 Smile | :) .

It looks like some VFREEBUSY constructs have been commented out, notably in the DDAY.iCal.ComponentFactory Build routine. It also looks like VFREEBUSY <i>is </i>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
GeneralRe: Sample or guidance on creating / reading VFREEBUSY Pin
Douglas Day10-Sep-10 4:30
memberDouglas Day10-Sep-10 4:30 
GeneralRe: Sample or guidance on creating / reading VFREEBUSY Pin
rw_architect16-Oct-10 5:19
memberrw_architect16-Oct-10 5:19 
GeneralRe: Sample or guidance on creating / reading VFREEBUSY Pin
Douglas Day26-Oct-10 10:33
memberDouglas Day26-Oct-10 10:33 
GeneralVB.Net Example1 variable declaration errors... Pin
rw_architect5-Sep-10 17:07
memberrw_architect5-Sep-10 17:07 
GeneralRe: VB.Net Example1 variable declaration errors... Pin
Douglas Day7-Sep-10 5:09
memberDouglas Day7-Sep-10 5:09 
GeneralRe: VB.Net Example1 variable declaration errors... Pin
Douglas Day7-Sep-10 5:18
memberDouglas Day7-Sep-10 5:18 
GeneralRe: VB.Net Example1 variable declaration errors... Pin
rw_architect9-Sep-10 12:55
memberrw_architect9-Sep-10 12:55 
GeneralRe: VB.Net Example1 variable declaration errors... Pin
Douglas Day9-Sep-10 13:00
memberDouglas Day9-Sep-10 13:00 
GeneralOT [ Bad tutorial link from your home page for DDay.Update] Pin
Garth J Lancaster15-Apr-10 0:18
memberGarth J Lancaster15-Apr-10 0:18 
GeneralWould be nice if article could be updated for version 0.80 Pin
manudurand14-Mar-10 1:52
membermanudurand14-Mar-10 1:52 
GeneralRe: Would be nice if article could be updated for version 0.80 Pin
Douglas Day14-Mar-10 10:30
memberDouglas Day14-Mar-10 10:30 
GeneralRe: Would be nice if article could be updated for version 0.80 Pin
manudurand14-Mar-10 16:21
membermanudurand14-Mar-10 16:21 
GeneralRe: Would be nice if article could be updated for version 0.80 Pin
Douglas Day14-Mar-10 17:01
memberDouglas Day14-Mar-10 17:01 
GeneralRe: Would be nice if article could be updated for version 0.80 Pin
manudurand14-Mar-10 19:50
membermanudurand14-Mar-10 19:50 
GeneralRe: Would be nice if article could be updated for version 0.80 Pin
Douglas Day15-Mar-10 5:24
memberDouglas Day15-Mar-10 5:24 
GeneralRe: Would be nice if article could be updated for version 0.80 Pin
Douglas Day15-Apr-10 6:06
memberDouglas Day15-Apr-10 6:06 
Questiongreat article Pin
jael3624-Nov-09 9:55
memberjael3624-Nov-09 9:55 
AnswerRe: great article Pin
Douglas Day29-Dec-09 10:01
memberDouglas Day29-Dec-09 10:01 
GeneralThanks Pin
Member 45575835-Aug-09 3:23
memberMember 45575835-Aug-09 3:23 
GeneralRe: Thanks Pin
Douglas Day5-Aug-09 4:12
memberDouglas Day5-Aug-09 4:12 
GeneralEvent.OccursOn and events that span two UTC days but is only 2 hours long Pin
i.i. wiin30-Apr-09 14:38
memberi.i. wiin30-Apr-09 14:38 
GeneralRe: Event.OccursOn and events that span two UTC days but is only 2 hours long Pin
Douglas Day30-Apr-09 14:53
memberDouglas Day30-Apr-09 14:53 
GeneralRe: Event.OccursOn and events that span two UTC days but is only 2 hours long Pin
i.i. wiin1-May-09 5:58
memberi.i. wiin1-May-09 5:58 
GeneralRe: Event.OccursOn and events that span two UTC days but is only 2 hours long Pin
Douglas Day1-May-09 6:03
memberDouglas Day1-May-09 6:03 
GeneralRe: Event.OccursOn and events that span two UTC days but is only 2 hours long Pin
i.i. wiin1-May-09 6:10
memberi.i. wiin1-May-09 6:10 
GeneralRe: Event.OccursOn and events that span two UTC days but is only 2 hours long Pin
Douglas Day1-May-09 6:55
memberDouglas Day1-May-09 6:55 
GeneralRe: Event.OccursOn and events that span two UTC days but is only 2 hours long Pin
i.i. wiin1-May-09 6:06
memberi.i. wiin1-May-09 6:06 
GeneralAttachment Problem Pin
Member 22397698-Dec-08 5:24
memberMember 22397698-Dec-08 5:24 
GeneralRe: Attachment Problem Pin
Douglas Day8-Dec-08 5:58
memberDouglas Day8-Dec-08 5:58 
GeneralView by Month, Week, Day Pin
Tommy Johnston6-Nov-08 6:10
memberTommy Johnston6-Nov-08 6:10 
GeneralError When Trying to Access Data in iCalendar File Pin
jdcurtis20-Jun-08 10:58
memberjdcurtis20-Jun-08 10:58 
QuestionFrequencyOccurrence use ? Pin
Elcado21-May-08 1:07
memberElcado21-May-08 1:07 
QuestionHow to create a Todo with an Alarm? Pin
LAURENT JP31-Jan-08 3:43
memberLAURENT JP31-Jan-08 3:43 
QuestionNet Framework 1.1 Support? Pin
CarlosSV17-Dec-07 6:19
memberCarlosSV17-Dec-07 6:19 
GeneralRe: Net Framework 1.1 Support? Pin
Douglas Day14-Jan-08 10:01
memberDouglas Day14-Jan-08 10:01 
QuestionAny VB examples? Pin
Gracie Diaz22-Oct-07 7:11
memberGracie Diaz22-Oct-07 7:11 
QuestionGreat job Pin
adc0814-Oct-07 22:27
memberadc0814-Oct-07 22:27 
AnswerRe: Great job Pin
rakeshg32121-Oct-07 18:42
memberrakeshg32121-Oct-07 18:42 
QuestionAPI Pin
Keith Farmer27-Sep-07 22:06
memberKeith Farmer27-Sep-07 22:06 
AnswerRe: API Pin
Douglas Day1-Oct-07 18:58
memberDouglas Day1-Oct-07 18:58 
GeneralThanks and a Question Pin
ChristopherN2-Aug-07 3:48
memberChristopherN2-Aug-07 3:48 
GeneralRe: Thanks and a Question Pin
Douglas Day2-Aug-07 15:50
memberDouglas Day2-Aug-07 15:50 
GeneralRe: Thanks and a Question [modified] Pin
ChristopherN6-Aug-07 3:53
memberChristopherN6-Aug-07 3:53 
GeneralRe: Thanks and a Question Pin
Douglas Day26-Aug-07 10:27
memberDouglas Day26-Aug-07 10:27 
GeneralRe: Thanks and a Question Pin
Douglas Day1-Oct-07 19:04
memberDouglas Day1-Oct-07 19:04 
GeneralRe: Thanks and a Question Pin
kneckedeck27-Mar-09 4:02
memberkneckedeck27-Mar-09 4:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 14 Apr 2010
Article Copyright 2007 by Douglas Day
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid