1
votes

I'm new to Graph API and need to get calendar events of the logged in user. I have got this to partly work following this article but I'm only seeing 10 events.

//get calendar events
            var startTime = DateTime.Today.ToString("s");
            var endTime = DateTime.Now.AddDays(7).ToString("s");
            var queryOptions = new List<Microsoft.Graph.QueryOption>()
            {
                new Microsoft.Graph.QueryOption("startDateTime", startTime),
                new Microsoft.Graph.QueryOption("endDateTime", endTime)
            };
            var calendarView = await graphClient.Me.Calendar.CalendarView
            .Request(queryOptions)
            .GetAsync();
            ViewData["Events"] = calendarView;

Looking at the properties of calenderView I can see there is: {[@odata.nextLink, https://graph.microsoft.com/beta/me/calendar/calendarView?startDateTime=2020-06-12T00%3a00%3a00&endDateTime=2020-06-19T12%3a49%3a37&$skip=10]}

I'm struggling to understand how to loop through the pages so I can return all events to my view.

My application is built around ASP Core 3.1 C# and Graph 3.7.0 and 1.20.1

Any assistance is much appreciated.

1

1 Answers

5
votes

10 is the default page size, which you can change by adding a .Top(N) call before .GetAsync(), where N is some number up to 1000. You may still have to page though, so you should be prepared to handle that.

You have two options to do this with the Graph .NET SDK. Given your code, where calendarView is the returned object from your initial request for 10 items:

Request each next page yourself

var nextPage = calendarView.NextPageRequest.GetAsync();

Use the SDK's page iterator class

The PageIterator class will go through each page for you, so all you have to do is give it a callback to do something with each event, then call IterateAsync.

var pageIterator = PageIterator<Event>.CreatePageIterator(
    graphClient, calendarView,
    (e) => {
        // Called for each event so you can process
        // Do something with the event here, add to a list,
        // etc.

        // Return true to keep iterating. You can
        // Return false to stop the iteration before exhausting all
        // pages
        return true;
    }
);
await pageIterator.IterateAsync();