Features

  • Export calendar events in iCalendar format
  • Subscribe using Microsoft Outlook
  • Subscribe using Mac OS X Calendar
  • Subscribe using Google Calendar
  • Import downloaded .ics file
  • Visual Studio 2017 sample project with source code

License

The source code of this sample project is licensed under the Apache License 2.0.

Subscribe vs. Import

When exporting the calendar events to other applications, such as Outlook or Mac OS X Calendar you can choose from two methods:

  • Import the calendar data
  • Subscribe to a remote calendar

Both methods require that your application makes the calendar data available in iCalendar format.

Importing the calendar data is a one-time operation - the users imports the data file and a copy of the events is created in the target calendar. Changing the data in the source will not have any effect on the imported events.

Subscribing to a remote calendar is similar to watching a remote RSS feed:

  1. The source must be made accessible on a web server (using a special URL).
  2. The calendar application regularly checks the source for changes. Any change made in the source will be reflected in the target applicaiton.
  3. The access to the remote calendar is read-only.

Generating iCalendar in ASP.NET WebForms

You can export the calendar data in the iCalendar format using DDay.iCal library.

C#

protected void Page_Load(object sender, EventArgs e)
{
  iCalendar iCal = new iCalendar();

  DataTable events = Db.LoadEvents(DateTime.Today.AddDays(-7), DateTime.MaxValue);

  foreach (DataRow dr in events.Rows)
  {
      Event evt = iCal.Create<Event>();
     
      evt.Start = new iCalDateTime((DateTime) dr["EventStart"]);
      evt.End = new iCalDateTime((DateTime) dr["EventEnd"]);
      evt.Description = (string)dr["EventName"];
  }

  iCalendarSerializer serializer = new iCalendarSerializer();
  string output = serializer.SerializeToString(iCal);

  Response.ContentType = "text/calendar";
  Response.Write(output);
  Response.End();

}

VB

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim iCal As New iCalendar()
    Dim events_Renamed As DataTable = Db.LoadEvents(Date.Today.AddDays(-7), Date.MaxValue)

    For Each dr As DataRow In events_Renamed.Rows
        Dim evt As [Event] = iCal.Create(Of [Event])()

        evt.Start = New iCalDateTime(DirectCast(dr("EventStart"), Date))
        evt.End = New iCalDateTime(DirectCast(dr("EventEnd"), Date))
        evt.Description = DirectCast(dr("EventName"), String)
    Next dr

    Dim serializer As New iCalendarSerializer()
    Dim output As String = serializer.SerializeToString(iCal)

    Response.ContentType = "text/calendar"
    Response.Write(output)
    Response.End()
End Sub

This sample code will include all events from the past 7 days and all future events.

You can also export a single event or events from a specified date range.

Sample iCalendar File

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//ddaysoftware.com//NONSGML DDay.iCal 1.0//EN
BEGIN:VEVENT
DESCRIPTION:Event 1
DTEND:20150915T133000
DTSTAMP:20150920T171917Z
DTSTART:20150915T100000
SEQUENCE:0
UID:f35e10b2-ef38-4c32-95af-ade03966e79c
END:VEVENT
BEGIN:VEVENT
DESCRIPTION:Event 2
DTEND:20150914T150000
DTSTAMP:20150920T171917Z
DTSTART:20150914T103000
SEQUENCE:0
UID:1de6f046-0a24-45b2-abca-dd3e68bc9483
END:VEVENT
BEGIN:VEVENT
DESCRIPTION:Event 3
DTEND:20150916T153000
DTSTAMP:20150920T171917Z
DTSTART:20150916T130000
SEQUENCE:0
UID:269c4f92-7b3e-48ed-a86a-aa26e429d364
END:VEVENT
BEGIN:VEVENT
DESCRIPTION:Event 4
DTEND:20150917T170000
DTSTAMP:20150920T171917Z
DTSTART:20150917T123000
SEQUENCE:0
UID:bd12fa1f-36ec-4e06-bc27-1fe07dfb61c5
END:VEVENT
END:VCALENDAR

Subscribe using Google Calendar

icalendar-google-calendar.png

Create a Google Calendar subscribe link using https://www.google.com/calendar/render?cid=TARGET.

<a href="https://www.google.com/calendar/render?cid=<%= Server.UrlEncode(AbsoluteUrl("~/iCalendarFeed.aspx")) %>">Subscribe using Google Calendar</a>

Sample URL output for http://calendar.daypilot.com/mycalendar/:

<a href="https://www.google.com/calendar/render?cid=http%3a%2f%2fcalendar.daypilot.com%2fmycalendar%2f">Subscribe using Google Calendar</a>

Subscribe using Microsoft Outlook

icalendar-outlook.png

Create an Outlook calendar subscribe link using webcal://TARGET URL. Use the URL of the iCalendar feed and replace http:// with webcal:// and https:// with webcals://.

<a href="<%= WebcalUrl("~/iCalendarFeed.aspx") %>">Subscribe using the Default Desktop Application (Outlook, Mac OS X Calendar)</a>

Sample URL output for http://calendar.daypilot.com/mycalendar/:

webcal://calendar.daypilot.com/mycalendar/

Subscribe using Mac OS X Calendar

icalendar-mac-os-x-calendar.png

Create an OS X Calendar subscribe link using webcal://TARGET URL. Use the URL of the iCalendar feed and replace http:// with webcal:// and https:// with webcals://.

<a href="<%= WebcalUrl("~/iCalendarFeed.aspx") %>">Subscribe using the Default Desktop Application (Outlook, Mac OS X Calendar)</a>

Sample URL output for http://calendar.daypilot.com/mycalendar/:

webcal://calendar.daypilot.com/mycalendar/

Import .ics File

<a href="<%= AbsoluteUrl("~/iCalendarFeed.aspx") %>">Download .ics file</a>

The default "text/calendar" MIME type will invoke file download in most browsers.

The users can save this .ics file to disk and import into their calendar application.