0
votes

I am trying to create event for all day using o365 event rest api but getting error start and end time should be midnight I tried using below start date & End date for all day event e.g. Start Date: 01/01/2016 12:00 AM (dd/MM/yyyy) End Date: 02/01/2016 12:00 AM (dd/MM/yyyy) As api says there should be 24 hours gap for all day event I did the same way still its throwing error.

I tried different case for creating event but there is difference between the dates which I passed to rest api, I tried with passing time zone too but still there is difference.

using API 2.0 getting differnt issue. Incompatible type kinds were found. The type 'Microsoft.OutlookServices.DateTimeTimeZone' was found to be of kind 'Complex' instead of the expected kind 'Primitive'.

var startDt=new DateTime(2016, 1, 22, 00, 00, 0);
startDate.DateTime = startDt.ToString(dateTimeFormat);
startDate.TimeZone = timeZone;

DateTimeTimeZone endDate = new DateTimeTimeZone();
endDate.DateTime = startDt.AddDays(1).ToString(dateTimeFormat);
endDate.TimeZone = timeZone;

Event newEvent = new Event
{
Subject = "Test Event",
Location = location,
Start = startDate,
End = endDate,
Body = body
};

            try
            {
                // Make sure we have a reference to the Outlook Services client
                var outlookServicesClient = await AuthenticationHelper.EnsureOutlookServicesClientCreatedAsync("Calendar");

                // This results in a call to the service.
                await outlookServicesClient.Me.Events.AddEventAsync(newEvent);
                await ((IEventFetcher)newEvent).ExecuteAsync();
                newEventId = newEvent.Id;
            }
            catch (Exception e)
            {
                throw new Exception("We could not create your calendar event: " + e.Message);
            }
            return newEventId;
1

1 Answers

0
votes

In order to use the v2 API, you need the v2 library from NuGet (which it looks like you're doing) AND the v2 endpoint (which by the error you are not). The v2 library isn't compatible with the v1 endpoint, which is causing that error you're seeing.

In the v1 endpoint, Start and End were just simple ISO 8601 date/time strings. In v2, they are complex types.

In v2 you need to specify the start and end date/times with time zones, and you also need to set the IsAllDay property on the event to true.

OutlookServicesClient client = new OutlookServicesClient(
  new Uri("https://outlook.office.com/api/v2.0"), GetToken);

Event newEvent = new Event()
{
  Start = new DateTimeTimeZone() 
  { 
    DateTime = "2016-04-16T00:00:00", 
    TimeZone = "Pacific Standard Time" 
  },
  End = new DateTimeTimeZone() 
  { 
    DateTime = "2016-04-17T00:00:00", 
    TimeZone = "Pacific Standard Time" 
  },
  Subject = "All Day",
  IsAllDay = true
};

await client.Me.Events.AddEventAsync(newEvent);

To do it in v1, you need to calculate the appropriate offsets and include them in the ISO 8601 string (compensating for DST). So since we're in DST, Pacific is currently UTC-7 (instead of -8 in standard). You also need to set the StartTimeZone and EndTimeZone properties. So I'd do something like:

Event newEvent = new Event()
{
  Start = "2016-04-16T00:00:00-07:00", 
  End = "2016-04-17T00:00:00-07:00",
  StartTimeZone = "Pacific Standard Time",
  EndTimeZone = "Pacific Standard Time", 
  Subject = "All Day",
  IsAllDay = true
};