Search code examples
c#microsoft-graph-apimicrosoft-graph-sdksmicrosoft-graph-calendar

QueryOptions ignored when getting calendar events in MSGraph Dot Net


I'm trying to get the user's calendar events for today. So I added some query parameters but they're getting ignored and the graph client returns the user's events as if I didn't supply any parameters (startatetime):

var options = new QueryOption[]
{
    new QueryOption("startdatetime", DateTime.UtcNow.ToString("o")),
    new QueryOption("enddatetime", DateTime.UtcNow.AddDays(1).ToString("o")),
};
var events = await graphServiceClient
    .Me
    .Calendar
    .Events
    .Request(options)
    .GetAsync();

I tested it in the graph explorer and it works fine. But in the sdk, it returns calendar events that started before today.


Solution

  • Your code is the equivalent of calling:

    `/events?startdatetime={dateTime}&enddatetime={dateTime}`. 
    

    That is a valid endpoint, but you're passing invalid query params. What you're looking for is calendarView:

    `/calendarView?startdatetime={dateTime}&enddatetime={dateTime}`
    

    Using the SDK, this would look like this:

    var options = new QueryOption[]
    {
        new QueryOption("startDateTime", DateTime.UtcNow.ToString("o")),
        new QueryOption("endDateTime", DateTime.UtcNow.AddDays(1).ToString("o")),
    };
    
    var events = await graphServiceClient
        .Me
        .CalendarView
        .Request(options)
        .GetAsync();