updating a calender event using msgraph sets the wrong timezone in start end properties

marco eggen 1 Reputation point
2022-06-27T09:09:50.023+00:00

I'm creating code to add and update calender events to a specific user outlook calender using MsGraph in C#.

I have created a class "CalenderEvent" which contains the properties I need to add/update the event. The start and end property are defined like

internal class EventDateTime  
{  
   public string DateTime { get; set; }  
   public string TimeZone { get; set; }  
}  

Adding an Event will fill CalenderEvent in a function and returns a StringContent like

var data = new CalendarEvent();  
data.Subject = subject;  
data.Body = new Body  
{  
    ContentType = "text",  
    Content = description  
};  
data.BodyPreview = string.IsNullOrEmpty(description) ? "" : description.Length > 15 ? description.Substring(0, 15) : description;  
data.Start = new EventDateTime  
{  
    DateTime = $"{startDate.Date.Add(startTime.TimeOfDay):O}",  
    TimeZone = "W. Europe Standard Time"  
};  
data.End = new EventDateTime  
{  
     DateTime = $"{startDate.Date.Add(startTime.TimeOfDay).AddMinutes(duration):O}",  
     TimeZone = "W. Europe Standard Time"  
};  
data.Sensitivity = "normal";  
data.Importance = "normal";  
  
string eventAsJson = JsonConvert.SerializeObject(data);  
var content = new StringContent(eventAsJson);  
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");  
content.Headers.Add("Prefer", "outlook.timezone=\"W. Europe Standard Time\"");  

As you can see I'm using the 'W. Europe Standard Time'

To add the event to Outlook I make a call through the httpClient

var task = $"users/{outlookUser.Id}/calendar/events";  
var content = FillEventData(activity);  
var resultToProcess = GetMsGraph(GetConfidentialClientApplication()).CallPost(task, content);  

GetMsGraph and GetConfidentialClientApplication are function to get a proper connection to MsGraph. CallPost will execute the task, with the content like:

var response = HttpClient.PostAsync($"{webApiUrl}v1.0/{task}", jsonData).Result;  

This will result in a new Event in the calender of the user, with the correct time and timezone

To update the event I have to call httpClient Patch. Because this is not available in our current Version I changed the call so it will execute a patch.

var task = $"users/{outlookUser.Id}/calendar/events/{outlookId}";  
var request = new HttpRequestMessage(new HttpMethod("PATCH"), $"{webApiUrl}v1.0/{task}")  
{  
    Content = jsonData                  
};  
var response = HttpClient.SendAsync(request).Result;  

The jsonData is filled similer as for creating the event.

The problem I have is that when I update the event, the timezone seems to be changed. In the calender view of outlook, the correct time is displayed, but opening the event will show the time of (in my case -2 hours) and a timezone "Coordinated Universal Time".

I have looked for a solution but could not find any that worked.

I tried to add a header in the httpClient. Same result.

I tried to add a header in the HttpRequestMessage. Same result.

I need this to be solved, because end users won't understand why the time not correct.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,556 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,222 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Glen Scales 4,431 Reputation points
    2022-07-06T00:31:46.873+00:00

    When you make the Patch request do you also set the Prefer header it seems to be missing in the code you posted ? eg

     content.Headers.Add("Prefer", "outlook.timezone=\"W. Europe Standard Time\"");  
    
    0 comments No comments