I'm writing a little proof-of-concept for a website where calendars/schedules should be exposed for users' inclusion in phone/web calendar applications.
First I'm trying with google calendar. My code aim to create a calendar with a singe event -- at any time it's "tomorrow at ten a.m.". I'm using dday.ical to produce ics.
I've written code below and published to http://mdftaxi.azurewebsites.net/
Ics can be downloaded from http://mdftaxi.azurewebsites.net/api/Calendar/1
Google appear to accept the calendar when I add with url above.
However -- nothing is shown in google calendar?! What am I doing wrong?!
Thanks for any help,
Anders, Denmark
public class CalendarController : ApiController
{
public HttpResponseMessage Get(int id)
{
var iCal = new iCalendar
{
Method = "PUBLISH",
Version = "2.0",
Name = "Taxi" + id
};
var evt = iCal.Create<Event>();
evt.Summary = "Test";
var dt = DateTime.Today.AddDays(1).AddHours(10);
evt.Start = new iCalDateTime(dt);
evt.Duration = TimeSpan.FromHours(1);
evt.Description = "This is body";
evt.Location = "location";
evt.IsAllDay = false;
evt.UID = Guid.NewGuid().ToString();
evt.Organizer = new Organizer("andersjuulsfirma@gmail.com");
var res = new DDay.iCal.Serialization.iCalendar.iCalendarSerializer().SerializeToString(iCal);
using (var file = new System.IO.StreamWriter(Path.GetTempPath() + "out.ics"))
{
file.WriteLine(res);
}
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(res)
};
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = "calendar.ics"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/calendar");
return result;
}
}
Edit: Found solution. At least it appears to solve the problem if I strip it down somewhat:
var iCal = new iCalendar
{
Version = "2.0"
};
for (var i = 0; i < 3; i++)
{
var evt = iCal.Create<Event>();
evt.Summary = "Test";
evt.Start = new iCalDateTime(DateTime.Today.AddDays(i).AddHours(20));
evt.Duration = TimeSpan.FromHours(1);
evt.Description = "This is body";
evt.IsAllDay = false;
evt.UID = Guid.NewGuid().ToString();
evt.Organizer = new Organizer("CN=John Doe:MAILTO:john.doe@example.com");
}
var res = new iCalendarSerializer().SerializeToString(iCal);
using (var file = new System.IO.StreamWriter(Path.GetTempPath() + "out.ics"))
{
file.Write(res);
}
return res;