Wednesday, November 21, 2012

MVC C# - Create iCal calendar ICS Feed using DDay.ical library


Following Code download the iCal file, Host this application in any URL and refer it as Feed in any Calendar application (Google calendar, Microsoft Calendar etc.,)


Requirements:
DDay.iCal library dll
MVC3 .Net 4 framework

public ActionResult iCalendar(string DownloadFileName)
{
DDay.iCal.iCalendar iCal = new DDay.iCal.iCalendar();

// Create the event, and add it to the iCalendar
Event evt = iCal.Create<Event>();

// Set information about the event
evt.Start = iCalDateTime.Today.AddHours(8);
evt.End = evt.Start.AddHours(18); // This also sets the duration
evt.Description = "The event description";
evt.Location = "Event location";
evt.Summary = "18 hour event summary";

// Set information about the second event
evt = iCal.Create<Event>();
evt.Start = iCalDateTime.Today.AddDays(5);
evt.End = evt.Start.AddDays(1);
evt.IsAllDay = true;
evt.Summary = "All-day event";

// Create a serialization context and serializer factory.
// These will be used to build the serializer for our object.
ISerializationContext ctx = new SerializationContext();
ISerializerFactory factory = new DDay.iCal.Serialization.iCalendar.SerializerFactory();
// Get a serializer for our object
IStringSerializer serializer = factory.Build(iCal.GetType(), ctx) as IStringSerializer;

string output = serializer.SerializeToString(iCal);
var contentType = "text/calendar";
var bytes = Encoding.UTF8.GetBytes(output);

return File(bytes, contentType, DownloadFileName);
}

21 comments:

  1. Thanks, appreciate this. Just what I was looking for.

    ReplyDelete
  2. What version of dday are you using? I only found the .Net 2.0 version and Visual studio deletes it whenever I do a clean.

    ReplyDelete
  3. I have downloaded DDay iCal from following URL and its working for MVC3

    http://www.ddaysoftware.com/Pages/Projects/DDay.iCal/

    ReplyDelete
    Replies
    1. That worked. Thank you I must have had a older copy.

      Delete
    2. Or download using NUGet package Installer

      Delete
  4. hi, hx for this usefull article
    im using dattimeoffset in my model, how can i convert that to ical datetime for the start and end dates?

    thnx in advance.
    best regards, Ahmad

    ReplyDelete
  5. Hi,

    I'm trying to make an calendar in MVC and i need to store data in my db.

    How can i get the attendees for an event/calendar date?

    ReplyDelete
  6. Try something like this

    var attendes = new List();

    IAttendee reqattendee = new DDay.iCal.Attendee("MAILTO:balajisrmv@gmail.com")
    {
    CommonName = "Balajiprasad",
    Role = "REQ-PARTICIPANT"
    };
    attendes.Add(reqattendee);

    IAttendee optattendee = new DDay.iCal.Attendee("MAILTO:leandro@somemail.com")
    {
    CommonName = "Leandro",
    Role = "OPT-PARTICIPANT"
    };
    attendes.Add(optattendee);

    if (attendes != null && attendes.Count > 0)
    {
    evt.Attendees = attendes;
    }

    ReplyDelete
    Replies
    1. Refer from http://stackoverflow.com/questions/7748246/dday-ical-adding-an-attendee

      Delete
    2. Thanks, really useful code! nice job.

      Best Regards.

      Delete
    3. Attendees are added but calendar event mail from iCloud is not coming. Any idea?

      Delete
  7. Hi,
    is it possible to create ics file with attachments. If possible how can i do it?
    any help is appreciated.
    Thankyou

    ReplyDelete
  8. Will this work within a razor view?

    ReplyDelete
  9. hy can u tell me how to delete icalendar event with start date and end date in asp.net c#

    ReplyDelete
  10. How can I send this ics file as mail attachment ?

    ReplyDelete
  11. Please share complete working sample.
    I don't find any documentation or sample on it.

    I'm trying to access Google Calendar.

    ReplyDelete
  12. I get the error, "System.IO.File is a type but is used like a variable" at "File" in the last line "return File(bytes, contentType, DownloadFileName);"

    ReplyDelete
  13. How to assign Start and End Date manually from our code

    ReplyDelete
  14. Why the File at last line giving error? Is there any class called File?

    ReplyDelete
  15. Delete event from calendar still NOT working.

    ReplyDelete