简体   繁体   English

图表 API 日历视图事件分页

[英]Graph API Calendar view events paging

I'm new to Graph API and need to get calendar events of the logged in user.我是 Graph API 的新手,需要获取登录用户的日历事件。 I have got this to partly work following this article but I'm only seeing 10 events.在这篇文章之后,我已经完成了部分工作,但我只看到了 10 个事件。

//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]}查看 calenderView 的属性,我可以看到:{[@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我的应用程序是围绕 ASP Core 3.1 C# 和 Graph 3.7.0 和 1.20.1 构建的

Any assistance is much appreciated.非常感谢任何帮助。

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. 10 是默认页面大小,您可以通过在.GetAsync()之前添加.Top(N)调用来更改它,其中 N 是不超过 1000 的某个数字。您可能仍然需要分页,因此您应该准备好处理那。

You have two options to do this with the Graph .NET SDK.您有两个选项可以使用图表 .NET SDK 来执行此操作。 Given your code, where calendarView is the returned object from your initial request for 10 items:给定您的代码,其中calendarView是您对 10 个项目的初始请求返回的 object :

Request each next page yourself自己请求每个下一页

var nextPage = calendarView.NextPageRequest.GetAsync();

Use the SDK's page iterator class使用 SDK 的页面迭代器 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 . PageIterator class 将为您通过每个页面 go ,因此您所要做的就是给它一个回调以对每个事件执行某些操作,然后调用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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM