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

C# Microsoft Graph API obtain specific calendar


I'm using the new Microsoft Graph Core API v1.20.0 in a Visual Studio 2019 v16.4.5 C# project. I want to obtain all calendar entries (Events) of each office365 calendar separately.

So far I know, I can retrieve all calendars with await graphClient.Me.Calendars.Request().GetAsync(); and Events with

string startDate = DateTime.Now.AddMonths(-1).ToString("s");
string endDate = DateTime.Now.AddYears(1).ToString("s");
var queryOptions = new List<QueryOption>()
{
    new QueryOption("startDateTime", startDate),
    new QueryOption("endDateTime", endDate),
};

var resultPage = await graphClient.Me.Events.Request(queryOptions)
    .OrderBy("createdDateTime DESC")
    .GetAsync();

But I don't know how to iterate over all Calendars and obtaining all Events for each Calendar.


Solution

  • Me.Events will just return events from the main calendar for a User.

    You will need to use Me.Calendars first as documented here https://learn.microsoft.com/en-us/graph/api/user-list-calendars?view=graph-rest-1.0&tabs=http

    var calendars = await graphClient.Me.Calendars
        .Request()
        .GetAsync();
    

    and for each calendar id you get back call list events https://learn.microsoft.com/en-us/graph/api/user-list-events?view=graph-rest-1.0&tabs=http

    GET /me/calendars/{id}/events
    

    which in the SDK would be

    graphClient.Me.Calendars["calendar-id"].Events..Request()
            .GetAsync();