I'm using Microsoft Graph .NET SDK to update outlook events. Following code updates the dates correctly, but it updates the Start
and End
times as four hours behind. When I go to my Outlook Calendar, it shows the changed times as 04:30
for Start and 05:30
for End instead of
showing 08:30
for Start and 09:30
for End. Question: Why? Remark: I'm in US East time zone but I don't think this code has anything to do with it unless I'm missing something here.
Code:
The values of authProvider
and "{id}"
varibles are not that relevant to the issue as the code with the real values works fines as it does update the event without error.
.....
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var @event = new Event
{
Subject = "Test subject",
Body= new ItemBody { Content = "Test body content"}
Start = new DateTimeTimeZone { DateTime = "2020-08-29T08:30:00.0000000", TimeZone = "UTC" }
End = new DateTimeTimeZone { DateTime = "2020-08-29T09:30:00.0000000", TimeZone = "UTC" }
};
await graphClient.Me.Events["{id}"]
.Request()
.UpdateAsync(@event);
If your updating the appointments and setting the TimeZone to UTC and then viewing the same appointment where the Outlook timezone in Eastern then that behavior sounds correct. As a general rule you should check the TimeZone of the appointment (or the Mailbox your modifying) and then match that in your update (especially if your dealing with multiple timezone). The other thing that can affect Time Zones is the Outlook.Prefer Header https://learn.microsoft.com/en-us/graph/api/user-list-events?view=graph-rest-1.0&tabs=http eg to set this
List<Option> options = new List<Option> { new HeaderOption("Prefer", "outlook.timezone=\"Eastern Time\"") };
await graphClient.Me.Events["{id}"]
.Request(options)
.UpdateAsync(@event);
Additional
Okay here's a quick unit test I created that creates an appointment and then updates it. In Outlook this shows me the corrected updated Datetime in Eastern Standard Time. If i substitute UTC for Eastern Standard Time in the Event details then it changes the Meeting Time Zone to UTC so will shift the time also (which i think is your issue if you are use UTC in the event). I would suggest you take a look at the appointment with the Outlook Desktop client as well as it will show you the TimeZone associated with the appointment where the web client just gives you the adjusted value.
List<Option> options = new List<Option> { new HeaderOption("Prefer", "outlook.timezone=\"Eastern Standard Time\"") };
DateTimeTimeZone start = new DateTimeTimeZone { DateTime = "2020-08-29T08:30:00.0000000", TimeZone = "UTC" };
var @event = new Event
{
Subject = "Test subject",
Body = new ItemBody { Content = "Test body content" },
Start = new DateTimeTimeZone { DateTime = "2020-08-29T07:30:00.0000000", TimeZone = "Eastern Standard Time" },
End = new DateTimeTimeZone { DateTime = "2020-08-29T08:30:00.0000000", TimeZone = "Eastern Standard Time" }
};
var newEvent = await GraphServiceClient.Me.Events
.Request(options)
.AddAsync(@event);
@event = new Event
{
Subject = "Updated subject",
Body = new ItemBody { Content = "Test body content" },
Start = new DateTimeTimeZone { DateTime = "2020-08-29T08:30:00.0000000", TimeZone = "Eastern Standard Time" },
End = new DateTimeTimeZone { DateTime = "2020-08-29T09:30:00.0000000", TimeZone = "Eastern Standard Time" }
};
await GraphServiceClient.Me.Events[newEvent.Id]
.Request(options)
.UpdateAsync(@event);