I'm using the graph client sdk. Making a call to CalendarView with startDateTime
= 2019-11-20T10:00:00.0000000
and endDateTime
= 2019-11-20T23:00:00.0000000
.
Before I go any further, none of the Events are all day. Many are reoccurring, so this is why I'm not using the Events endpoint, but using CalendarView.
I have the timezone set for the calendar events by setting prefer, outlook.timezone="Eastern Standard Time"
-- No problem there. I can see the event start and end times in Eastern Standard Time.
However, the events are not correct. I have an event that starts at 8:00 am EST and one that starts at 6:30 pm EST.
The 8:00 am meeting shows--when it should not because my start criteria are 10 am. Not a big deal, but it may be because it ends at 10 am.
The 6:30 pm meeting does NOT show and it should because my end time is 11:00 pm. This is a problem.
If I move the 6:30 meeting up to 6:00 it shows in the collection.
So I'm thinking since I am UTC -5
, the startDateTime
is locked in UTC time. It does not go by the header timezone. I know that the header timezone as mentioned above is working, because the actual start/end of my events will show Eastern if I have the header set, and UTC if I do not.
I can't find anything that allows me to force the calendarview?$select=subject,start, end&startDateTime=2019-11-20T10:00:00.0000000&endDateTime=2019-11-20T23:30:00.0000000
to use my local time.
I played around with this in Graph Explorer and found out that I can put -5
after the time, and it appears to work. I still get the 8:00 am event, but I also get my 6:30 event. I don't want to do this in my code because then I'll be forcing other users to use my timezone.
Does anyone know what setting can be changed to force the CalendarView to use local time for the parameters? I apologize, I'm new to graph client so I may not be using the correct vocabulary.
Code:
GraphServiceClient graphClient = new GraphServiceClient(AuthProvider);
DateTime d = DateTime.Now.ToLocalTime();
DateTime.SpecifyKind(d, DateTimeKind.Unspecified).ToString("o");
string MicrosoftDateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffff";
var start = d.ToString(MicrosoftDateTimeFormat);
var end = d.ToString("yyyy'-'MM'-'dd'T'23:59:59.0"); //"2019-07-24T23:00:00.0";//
start = "2019-11-20T10:00:00.0000000"; //for debug only
end = "2019-11-20T23:00:00.0000000"; //for debug only
List<Option> options = new List<Option>
{
new QueryOption("startDateTime", start),
new QueryOption("endDateTime", end),
new QueryOption("orderby", "start/dateTime")
};
var events = await graphClient.Me.CalendarView
.Request(options)
.Header("Prefer", "outlook.timezone=\"" + TimeZoneInfo.Local.Id + "\"")
.Select(e => new
{
e.Subject,
e.Body,
e.BodyPreview,
e.Organizer,
e.Attendees,
e.Start,
e.End,
e.Location
})
.GetAsync();
The prefer: outlook.timezone="Eastern Standard Time"
header only affects the API response. Your header will ensure the results are returned in EST but that timezone transformation happens after the results are retrieved. On the backend, every Event is stored as UTC -0
.
In order to request events within the bounds of a given timezone, that timezone needs to be encoded into the startTime
and endTime
values using the ISO 8601 format:
YYYY-MM-DDThh:mm:ss±hh:mm
or more simply put:
{date}T{time}±{offset}
So in order to transform your startDateTime
of 2019-11-20T10:00:00.0000000
to Easter Standard it would be 2019-11-20T10:00:00-5
.
In C#, the DateTimeOffset
object can generate the proper format for you using .ToString("O")
:
var tzOffset = new TimeSpan(-5, 0, 0);
var dateTime = new DateTimeOffset(2019, 11, 20, 10, 0, 0, tzOffset);
List<Option> options = new List<Option>
{
new QueryOption("startDateTime", dateTime.ToString("O")),
new QueryOption("endDateTime", dateTime.AddHours(24).ToString("O"))
};
You can also lookup the correct UTC offset for a given Time Zone using the TimeZoneInfo
class. This is generally a good idea since Time Zones change far more frequently than you'd imagine:
TimeSpan tzOffset = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time").BaseUtcOffset;
DateTimeOffset dateTime = new DateTimeOffset(2019, 11, 20, 10, 0, 0, tzOffset);
List<Option> options = new List<Option>
{
new QueryOption("startDateTime", dateTime.ToString("O")),
new QueryOption("endDateTime", dateTime.AddHours(24).ToString("O"))
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With