0

I have an C# application, in which I have the need for creating some "advanced" recurring events.

For example, I need to create a task every monday, wednesday and sunday every year in june, july and august.

Or every second week throughout the year, I want to create a task every monday morning, every monday evening and every friday noon, but not in december.

This might seem silly, but it reflects some needs in the real world.

So is this even possible? I cant seem to find any tools or technologies that supports this kind of recurrence, without splitting it up in several ocurrences.

Farsen
  • 1,387
  • 3
  • 21
  • 48
  • 1
    Use the operating systems task scheduler? or take a look @ https://www.quartz-scheduler.net/features.html – Alex K. Feb 23 '17 at 14:54
  • Thanks for your comment. As far as I can see, quartz.net doesent support the recurrence functionality I stated above.. – Farsen Feb 25 '17 at 13:16

1 Answers1

0

This is not silly at all :). I am also working on the same. I am using RRULE property of iCalendar to achieve this functionality. iCalendar is a standard method of transferring calendar information between computer systems. You can go through with http://www.kanzaki.com/docs/ical/rrule.html and https://icalendar.org/ to understand the all recurring scenario (daily, weekly, monthly, yearly).

Code for creating single and recurring event using iCal properties

    public string MakeRecurringEvent(AppointmentEvent message)
    {
    string startDay = "VALUE=DATE:" + GetFormatedDate(message.StartDate) + "T" + GetFormattedTime(message.StartTime);
    string endDay = "VALUE=DATE:" + GetFormatedDate(message.StartDate) + "T" + GetFormattedTime(message.EndTime);

    var organizer = "Listen To Customer Site Admin";
    var attendee = message.Attendee;
    string filePath = string.Empty;
    string path = HttpContext.Current.Server.MapPath(@"..\Content\Calendar\iCal\");
    filePath = path + message.Subject + ".ics";
    writer = new StreamWriter(filePath);

    writer.WriteLine("BEGIN:VCALENDAR");
    writer.WriteLine("VERSION:2.0");
    writer.WriteLine("METHOD:REQUEST");
    writer.WriteLine("PRODID:-//hacksw/handcal//NONSGML v1.0//EN");
    writer.WriteLine("BEGIN:VEVENT");     
    writer.WriteLine("ORGANIZER;CN=\"{0}\":MAILTO:{1}", organizer, message.Organizer);
    string[] arrAttendees = message.Attendee.Split(',');
    for (int countAtt = 0; countAtt < arrAttendees.Length - 1; countAtt++)
    {
        writer.WriteLine("ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=\"{0}\":MAILTO:{1}", attendee,
        arrAttendees[countAtt], arrAttendees[countAtt]);
    }        
    writer.WriteLine("DTSTART;" + startDay);
    writer.WriteLine("DTEND;" + endDay);
    if (message.IsRecurring)
    {
        switch (message.Frequency)
        {
            case "Daily":
                writer.WriteLine("RRULE:FREQ=DAILY;UNTIL={0}", GetFormatedDate(message.EndDate));
                break;
            case "Weekly":
                writer.WriteLine("RRULE:FREQ=WEEKLY;UNTIL={0}", GetFormatedDate(message.EndDate));
                break;
            case "Monthly1":
                writer.WriteLine("RRULE:FREQ=MONTHLY;INTERVAL={0};UNTIL={1}",message.RecurranceMonth, GetFormatedDate(message.EndDate));
                break;
            case "Monthly2":
                writer.WriteLine("RRULE;TZID=America/New_York:FREQ=MONTHLY;UNTIL={0};BYDAY=\"{1}\"", GetFormatedDate(message.EndDate)+"T"+ GetFormattedTime(message.StartTime), "1FR");                   
                break;
        }

    }


    writer.WriteLine("LOCATION:" + message.Location);
    writer.WriteLine("UID:{0}", Guid.NewGuid());
    writer.WriteLine("DESCRIPTION", message.From);
    writer.WriteLine("SUMMARY:" + message.Subject);      
    writer.WriteLine("ORANIZER:MAILTO:{0}", message.Organizer);        
    writer.WriteLine("X-ALT-DESC:FMTTYPE=text/html:{0}", message.Body);
    writer.WriteLine("END:VEVENT");
    writer.WriteLine("END:VCALENDAR");
    writer.Close();
    return filePath;
}
manika
  • 187
  • 2
  • 13
  • Thanks for your answer :) the RRULE seems quite flexible. But have you build a UI for creating such advanced rules? – Farsen Feb 25 '17 at 13:18
  • Yes. i have created screen for the same and i am also saving single and recurring event into my application's database. I have taken recurrence pattern reference from outlook calendar event/appointment. I am posting recurrence pattern which i created for my application. – manika Feb 25 '17 at 14:57