Creating a calendar file the easy way

I tend to eat my own dog food and found myself needing to create a calendar file which I thought was a trivial task. In hindsight it wasn't!

This post contains a simple code snippet to do just that, without the need to install Nuget packages etc.

The Problem

I needed to create a calendar file that would work in most applications, the major one being the UWP Calendar App on Windows 10.

The Solution

An ICS file is basically a text file containing all the information needed, it just needed to be formatted to be recognised by most applications.

The How

The Date Format

Calendar files need the date values in the ISO 8601 standard.
DateTime.UtcNow.ToString("o");
The DateTime formatting above will achieve this.


The Contents Of An ICS File

Every value in the file should be on a new line, you can use StringBuilder if you want to.

An example of an all day event

var dateStart = DateTime.UtcNow.ToString("yyyyMMdd");
var dateEnd = DateTime.UtcNow.AddDays(1).ToString("yyyyMMdd");
var file = $@"BEGIN:VCALENDAR
PRODID:-//Microsoft Corporation//Outlook 16.0 MIMEDIR//EN
VERSION:2.0
METHOD:REQUEST
X-MS-OLK-FORCEINSPECTOROPEN:TRUE
BEGIN:VEVENT
CLASS:PUBLIC
DESCRIPTION:{description}
DTSTART;VALUE=DATE:{dateStart}
DTEND;VALUE=DATE:{dateEnd}
LOCATION:{location}
PRIORITY:5
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:{summary}
TRANSP:OPAQUE
X-MICROSOFT-CDO-BUSYSTATUS:TENTATIVE
X-MICROSOFT-CDO-IMPORTANCE:1
X-MICROSOFT-CDO-INTENDEDSTATUS:BUSY
X-MICROSOFT-DISALLOW-COUNTER:FALSE
X-MS-OLK-AUTOSTARTCHECK:FALSE
X-MS-OLK-CONFTYPE:0
BEGIN:VALARM
TRIGGER:-PT15M
ACTION:DISPLAY
DESCRIPTION:Reminder
END:VALARM
END:VEVENT
END:VCALENDAR
";

An example of a duration event

var dateStart = DateTime.UtcNow.ToString("o");
var dateEnd = DateTime.UtcNow.AddHours(2).ToString("o");
var file = $@"BEGIN:VCALENDAR
PRODID:-//Microsoft Corporation//Outlook 16.0 MIMEDIR//EN
VERSION:2.0
METHOD:REQUEST
X-MS-OLK-FORCEINSPECTOROPEN:TRUE
BEGIN:VEVENT
CLASS:PUBLIC
DESCRIPTION:{description}
DTSTART;VALUE=DATE:{dateStart}
DTEND;VALUE=DATE:{dateEnd}
LOCATION:{whereAmI}
PRIORITY:5
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:{summary}
TRANSP:OPAQUE
X-MICROSOFT-CDO-BUSYSTATUS:TENTATIVE
X-MICROSOFT-CDO-IMPORTANCE:1
X-MICROSOFT-CDO-INTENDEDSTATUS:BUSY
X-MICROSOFT-DISALLOW-COUNTER:FALSE
X-MS-OLK-AUTOSTARTCHECK:FALSE
X-MS-OLK-CONFTYPE:0
BEGIN:VALARM
TRIGGER:-PT15M
ACTION:DISPLAY
DESCRIPTION:Reminder
END:VALARM
END:VEVENT
END:VCALENDAR
";

Putting It Together

byte[] filedata = Encoding.ASCII.GetBytes(file);
Will give you a byte array which you can then use to create a file for download or attach to an email.
Also remember the MIME type of a calendar file is text/calendar.