Hello!

2 Days ago I had one awesome idea: create an ICS file as part of an C# app. The strange thing, is that when you are looking for this topic in the internet, you get some examples which involves outlook automation. This scares me, because in addition to having a very restricted security model, use Office Automation in a server is kind of a suicide.

As well, I went to sources and found the definition of Internet Calendar, in addition to a plain text like the following example:

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
UID:uid1@example.com
DTSTAMP:19970714T170000Z
ORGANIZER;CN=John Doe:MAILTO:john.doe@example.com
DTSTART:19970714T170000Z
DTEND:19970715T035959Z
SUMMARY:Bastille Day Party
END:VEVENT
END:VCALENDAR

If you create an empty text file with ICS extension, and you edit it later with a text editor, you can paste this content, and you’re getting closer. So I created a small routine in C# that generates the ICS content for me:

   1: private const string DateFormat = "yyyyMMddTHHmmssZ";

   2:  

   3: public string GenerateIcsContent(DateTime dateStart, 

   4:               DateTime dateEnd, string email, string location, 

   5:               string summary, string description)

   6:         {

   7:             var s = dateStart.ToUniversalTime().ToString(DateFormat);

   8:             var e = dateEnd.ToUniversalTime().ToString(DateFormat);

   9:             var iCal = string.Format(@"BEGIN:VCALENDAR

  10: VERSION:2.0

  11: METHOD:PUBLISH

  12: BEGIN:VEVENT

  13: ORGANIZER:MAILTO:{0}

  14: DTSTART:{1}

  15: DTEND:{2}

  16: LOCATION:{3}

  17: UID:{4}

  18: DTSTAMP:{4}

  19: SUMMARY:{5}

  20: DESCRIPTION:{6}

  21: PRIORITY:5

  22: CLASS:PUBLIC

  23: END:VEVENT

  24: END:VCALENDAR", email, s, e, location, 

  25:                 DateTime.Now.ToUniversalTime().ToString(DateFormat), 

  26:                 summary, description);

  27:             return iCal;

  28:         }

Then I save this content into a text file with ICS extension and that’s it !!! Open-mouthed smile

Saludos @ La Finca

El Bruno

image image image Google

Leave a comment

Discover more from El Bruno

Subscribe now to keep reading and get access to the full archive.

Continue reading