Skip to content
axunonb edited this page Oct 15, 2024 · 8 revisions

Create a simple calendar event

var date = new CalDateTime(2024, 10, 15, 11, 0, 0);
var calendarEvent = new CalendarEvent
{
    // If Name property is used, it MUST be RFC 5545 compliant
    Summary = "Event Title", // Should always be present
    Description = "Event description goes here", // optional
    Start = new CalDateTime(date),
    End = new CalDateTime(date.AddHours(2)),
};

var calendar = new Ical.Net.Calendar();
calendar.Events.Add(calendarEvent);
calendar.AddTimeZone(new VTimeZone("Europe/Copenhagen")); // TZ should be added
var serializer = new CalendarSerializer();
var serializedCalendar = serializer.SerializeToString(calendar);
Console.WriteLine(serializedCalendar);

Output (DTSTAMP and UID will differ):

BEGIN:VCALENDAR
PRODID:-//github.com/ical-org/ical.net//NONSGML ical.net 4.0//EN
VERSION:2.0
BEGIN:VEVENT
DESCRIPTION:Event description goes here
DTEND:20241015T130000
DTSTAMP:20241015T110000
DTSTART:20241015T110000
SEQUENCE:0
SUMMARY:Event Title
UID:a4982558-e1aa-412e-8610-e2102fe0e616
END:VEVENT
BEGIN:VTIMEZONE
TZID:Europe/Copenhagen
X-LIC-LOCATION:Europe/Copenhagen
END:VTIMEZONE
END:VCALENDAR

Create a calendar with a recurring event

var now = DateTime.Now;
var later = now.AddHours(1);

//Repeat daily for 5 days
var rrule = new RecurrencePattern(FrequencyType.Daily, 1) { Count = 5 };

var e = new CalendarEvent
{
    Start = new CalDateTime(now),
    End = new CalDateTime(later),
    RecurrenceRules = new List<RecurrencePattern> { rrule },
};

var calendar = new Calendar();
calendar.Events.Add(e);

var serializer = new CalendarSerializer();
var serializedCalendar = serializer.SerializeToString(calendar);

Output:

BEGIN: VCALENDAR
VERSION:2.0
PRODID: -//github.com/ical-org/ical.net//NONSGML ical.net 4.0//EN
BEGIN:VEVENT
DTEND:20160704T172520
DTSTAMP:20160704T162520
DTSTART:20160704T162520
RRULE:FREQ=DAILY;COUNT=5
SEQUENCE: 0
UID: f4693a88-0a57-4761-b949-8822b8a507d2
END:VEVENT
END:VCALENDAR