c# - Microsoft Exchange Folders.findItems 结果限制为 1000

标签 c# exchangewebservices

我正在尝试从 Microsoft Exchange 的联系人文件夹中获取联系人列表。
即使文件夹中有更多项目,结果也只返回 1000 项。
这是我的代码。

FindFoldersResults r = service.FindFolders(new FolderId(WellKnownFolderName.PublicFoldersRoot), new FolderView(10));
     Folder folder = getFolder("test", r.Folders);
     ContactsFolder contactsfolder = ContactsFolder.Bind(service, new FolderId(folder.Id.ToString()), new PropertySet(BasePropertySet.IdOnly, FolderSchema.TotalCount));        
     FindItemsResults<Item> items = folder.FindItems(new ItemView(contactsfolder.TotalCount));   

我怎样才能让它返回所有项目?

最佳答案

正如 Jason 引用的文章所建议的那样,分页是关键。这是我针对 Office365 Exchange 服务器使用的代码,用于获取给定文件夹中所有电子邮件的列表(超过 20,000 封电子邮件,在页面大小为 100 时工作速度非常快):

            // via https://msdn.microsoft.com/en-us/library/office/dn592093(v=exchg.150).aspx
            int pageSize = 100;
            int offset = 0;
            ItemView view = new ItemView(pageSize + 1, offset);
            view.PropertySet = new PropertySet(ItemSchema.Subject, ItemSchema.DateTimeSent);
            view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);
            view.Traversal = ItemTraversal.Shallow;

            bool moreItems = true;
            ItemId anchorId = null;

            while (moreItems)
            {
                FindItemsResults<Item> results = service.FindItems(buildsFolderId, view);
                moreItems = results.MoreAvailable;
                if (moreItems && anchorId != null)
                {
                    // Check the first result to make sure it matches
                    // the last result (anchor) from the previous page.
                    // If it doesn't, that means that something was added
                    // or deleted since you started the search.
                    if (results.Items.First<Item>().Id != anchorId)
                    {
                        Console.Error.WriteLine("The collection has changed while paging. Some results may be missed.");
                    }
                }

                if (moreItems)
                {
                    view.Offset += pageSize;
                }

                anchorId = results.Items.Last<Item>().Id;

                // Because you’re including an additional item on the end of your results
                // as an anchor, you don't want to display it.
                // Set the number to loop as the smaller value between
                // the number of items in the collection and the page size.
                int displayCount = results.Items.Count > pageSize ? pageSize : results.Items.Count;

                for (int i = 0; i < displayCount; i++)
                {
                    Item item = results.Items[i];

                    Console.WriteLine("[" + item.DateTimeSent + "] " + item.Subject);
                }

                Console.Error.WriteLine("Current offset: {0}/{1}", view.Offset, folder.TotalCount);
            }

关于c# - Microsoft Exchange Folders.findItems 结果限制为 1000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12200109/

相关文章:

java - 尝试使用 MS ews java API 发送邮件时。尽管身份验证完美,但接收 PKIX 路径失败异常

exchangewebservices - 使用 EWS 获取原始电子邮件文本(标题、正文和编码附件)

c# - 如何在单个 Razor View 中编辑多个模型

c# - Appointment.Save 和 Appointment.Update 始终将 IsMeeting 设置为 true

c# - Linq-to-SQL,通过 Expression<Func<T, T>> 在查询语法中选择方法

c# - 获取 EF 投影中从刻度转换的 TimeSpan 的 TimeSpan 属性

exchange-server - 什么是 IPM.Note,它的用途是什么?

azure - Azure 应用程序 list 中的 "encryptedSecretValue"参数中应该包含什么?

c# - 在 asp.net c# 环境中以上一页作为内容发送电子邮件

c# - 是否可以将默认内容类型设置为 "application/json;v=2.0"