Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

c# - Microsoft Graph Event Resource type: Updating Start and End datetime issue

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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://docs.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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...