Advertisement
EmilySamantha80

ICS calendar item creation

Dec 8th, 2016
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.98 KB | None | 0 0
  1. // Title:  ICS calendar item creation
  2. // Author: Emily Heiner
  3. // Date:   2016-12-08
  4. // This code is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported license.
  5. // You are free to share and adapt this code for any purposes, commerical and non-commercial,
  6. // as long as appropriate credit is given, you indicate if changes were made, and you share your
  7. // modified code. License details can be found at https://creativecommons.org/licenses/by-sa/3.0/
  8.  
  9. using System;
  10.  
  11. namespace ESH.Utility
  12. {
  13.     public static class Email
  14.     {
  15.         /// <summary>
  16.         /// Creates an ICS calendar item
  17.         /// </summary>
  18.         /// <param name="startDate">Calendar item Start Date/Time</param>
  19.         /// <param name="endDate">Calendar item End Date/Time</param>
  20.         /// <param name="summary">Calendar item summary</param>
  21.         /// <param name="location">Calendar item Location</param>
  22.         /// <param name="notificationTime">Notification time examples: "30M" (30 Minutes), "1H" (1 Hour), "24H" (24 Hours)</param>
  23.         /// <returns>String containing the ICS calendar item</returns>
  24.         public static string CreateICS(DateTime startDate, DateTime endDate, string summary, string location, string notificationTime)
  25.         {
  26.             string ics = "";
  27.             ics += "BEGIN:VCALENDAR" + Environment.NewLine;
  28.             ics += "VERSION:2.0" + Environment.NewLine;
  29.             ics += "PRODID:-//hacksw/handcal//NONSGML v1.0//EN" + Environment.NewLine;
  30.             ics += "BEGIN:VEVENT" + Environment.NewLine;
  31.             ics += "UID:" + Guid.NewGuid() + Environment.NewLine;
  32.             ics += "DSTAMP;TZID=/US/Pacific:" + ConvertToICSDateTime(DateTime.Now) + Environment.NewLine;
  33.             ics += "DTSTART;TZID=/US/Pacific:" + ConvertToICSDateTime(startDate) + Environment.NewLine;
  34.             ics += "DTEND;TZID=/US/Pacific:" + ConvertToICSDateTime(endDate) + Environment.NewLine;
  35.             ics += "SUMMARY:" + summary + Environment.NewLine;
  36.             ics += "LOCATION:" + location + Environment.NewLine;
  37.             if (notificationTime != null)
  38.             {
  39.                 ics += "BEGIN:VALARM" + Environment.NewLine;
  40.                 //Notification time examples: 30M (30 Minutes), 1H (1 Hour), 24H (24 Hours)
  41.                 ics += "TRIGGER:-PT" + notificationTime + Environment.NewLine;
  42.                 ics += "ACTION:DISPLAY" + Environment.NewLine;
  43.                 ics += "END:VALARM" + Environment.NewLine;
  44.             }
  45.             ics += "END:VEVENT" + Environment.NewLine;
  46.             ics += "END:VCALENDAR" + Environment.NewLine;
  47.  
  48.             return ics;
  49.         }
  50.  
  51.         /// <summary>
  52.         /// Converts a DateTime object into an ICS date/time string
  53.         /// </summary>
  54.         /// <param name="input">DateTime object to convert</param>
  55.         /// <returns>ICS date/time string</returns>
  56.         public static string ConvertToICSDateTime(DateTime input)
  57.         {
  58.             return input.ToString("yyyyMMddTHmmss");
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement