简体   繁体   English

如何使用Exchange Web服务托管API列出这些文件夹中的所有文件夹和文件?

[英]How to list all folders and files in those folders using Exchange Web Services Managed API?

I just discovered the Exchanged Web Services Managed API and I've been playing around with it to try and gain an understanding because I believe it may provide a solution to an issue I've been working on. 我刚刚发现了Exchange Web Services Managed API,并且一直在尝试以了解它,因为我相信它可以为我一直在研究的问题提供解决方案。

Background 背景

I've read a lot of the documentation and have a general sense of things. 我已经阅读了很多文档,并对事物有一般的了解。 In particular, I set up something very similar to this LINK . 特别是,我设置了与此LINK非常相似的内容。

However, I want to display a folder, then display all of the related EmailMessage items after the folder, and so on and so forth through all folders/sub-folders. 但是,我要显示一个文件夹,然后在该文件夹之后显示所有相关的EmailMessage项目,依此类推,通过所有文件夹/子文件夹等等。 The code I've written so far, will list a folder, but it will list all of the emails that I returned instead of just those emails related to the folder. 到目前为止,我已编写的代码将列出一个文件夹,但是它将列出我返回的所有电子邮件,而不仅仅是与该文件夹相关的那些电子邮件。 I'm searching for unread items just to keep the list small for the time-being. 我正在搜索未读项目,只是为了使列表暂时保持较小。

Code

private static void GetAllItems(ExchangeService service)
    {
        List<Folder> lstFolderIds = new List<Folder>();

        // Fill list with all public folders and sub-folders
        GetAllFolders(service, lstFolderIds);

        List<EmailMessage> emails = new List<EmailMessage>();

        foreach(Folder folder in lstFolderIds)
        {
            SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
            ItemView view = new ItemView(int.MaxValue);
            view.PropertySet = PropertySet.IdOnly;

            FindItemsResults<Item> searchResults = service.FindItems(folder.Id, sf, view);

            if (searchResults.Items.Count > 0)
            {
                foreach (var item in searchResults.Items)
                {
                    try
                    {
                        if (item is EmailMessage)
                        {
                            emails.Add((EmailMessage)item);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exception: {0}", ex.Message);
                        Console.WriteLine("Stack Trace: {0}", ex.StackTrace);
                        Console.WriteLine("Source: {0}", ex.Source);
                        Console.WriteLine("HResult: {0}", ex.HResult);
                    }
                }
            }
        }

        PropertySet properties = (BasePropertySet.FirstClassProperties);
        service.LoadPropertiesForItems(emails, properties);

        foreach (Folder folder in lstFolderIds)
        {
            object folderName;
            object displayName;
            object childFolderCount;
            object unreadCount;
            object totalCount;

            folderName = folder.TryGetProperty(FolderSchema.ParentFolderId, out folderName) ? folder.ParentFolderId.FolderName.Value.ToString() : "N/A";
            displayName = folder.TryGetProperty(FolderSchema.DisplayName, out displayName) ? folder.DisplayName.ToString() : "N/A";
            childFolderCount = folder.TryGetProperty(FolderSchema.ChildFolderCount, out childFolderCount) ? folder.ChildFolderCount.ToString() : "N/A";
            unreadCount = folder.TryGetProperty(FolderSchema.UnreadCount, out unreadCount) ? folder.UnreadCount.ToString() : "N/A";
            totalCount = folder.TryGetProperty(FolderSchema.TotalCount, out totalCount) ? folder.TotalCount.ToString() : "N/A";

            Console.WriteLine("Parent Folder: {0}", folderName);
            Console.WriteLine("Folder Display Name: {0}", displayName);
            Console.WriteLine("Child Folder Count: {0}", childFolderCount);
            Console.WriteLine("Folder Unread Count: {0}", unreadCount);
            Console.WriteLine("Folder Total Count: {0}", totalCount);
            Console.WriteLine("---------------------------------------------------------");
            foreach (EmailMessage email in emails)
            {
                object parentFolderName;
                object subject;
                object retentionDate;

                parentFolderName = email.TryGetProperty(EmailMessageSchema.ParentFolderId, out parentFolderName) ? email.ParentFolderId.FolderName.Value.ToString() : "N/A";
                subject = email.TryGetProperty(EmailMessageSchema.Subject, out subject) ? email.Subject.ToString() : "N/A";
                retentionDate = email.TryGetProperty(EmailMessageSchema.RetentionDate, out retentionDate) ? retentionDate.ToString() : "N/A";

                Console.WriteLine("\tParent Folder Name: {0}", parentFolderName);
                Console.WriteLine("\tSubject: {0}", subject);
                Console.WriteLine("\tRetention Date: {0}", retentionDate);
                //}                    
            }

            Console.WriteLine("");
            Console.WriteLine("Press or select Enter...");
            Console.Read();
        }
    }

Part of the problem is that I can't figure out how to get the folder name for the folder or the parent folder name for the EmailMessage. 问题的部分原因是我不知道如何获取文件夹的文件夹名称或EmailMessage的父文件夹名称。 I can get the folder ID for the folder and the parent folder ID for the EmailMessage, but they are identical for all items that are returned, so it must not be right. 我可以获取该文件夹的文件夹ID和EmailMessage的父文件夹ID,但是对于返回的所有项目它们都是相同的,因此它一定不正确。

I read in this LINK that I may need to carry out an additional bind to relate the folders with the EmailMessage items, but I'm not exactly sure how to do that. 我在此LINK中阅读到,我可能需要执行附加绑定以将文件夹与EmailMessage项相关联,但是我不确定如何做到这一点。

Expected Output 预期产量

Parent Folder: 
Folder Display Name: Inbox
Child Folder Count: 1
Folder Unread Count: 2
Folder Total Count: 51
---------------------------------------------------------
    Parent Folder Name: Inbox
    Subject: New Coversheets for TPS Reports
    Retention Date: 10/31/2017 11:59:59 PM

    Parent Folder Name: Inbox
    Subject: Have you seen my stapler?
    Retention Date: 07/31/2017 11:59:59 PM

Parent Folder: 
Folder Display Name: Sent Items
Child Folder Count: 0
Folder Unread Count: 0
Folder Total Count: 27
---------------------------------------------------------
    No Un-Read Items

Questions 问题

  1. How do I list the folder hierarchy with related EmailMessages that match my search condition? 如何列出与搜索条件匹配的相关电子邮件消息的文件夹层次结构?
  2. Is the FolderId/ParentFolderId that I'm currently obtaining the Id for the MsgFolderRoot or is there any way to tell? 我当前正在获取MsgFolderRoot的ID的FolderId / ParentFolderId还是有什么办法可以告诉我?
  3. How do I obtain the Folder.FolderId.FolderName.Value and the EmailMessage.ParentFolderId.FolderName.Value? 如何获取Folder.FolderId.FolderName.Value和EmailMessage.ParentFolderId.FolderName.Value?
  4. If binding is necessary for #2, does anyone have an example or link they would share that illustrates the action? 如果绑定对于#2是必需的,那么是否有人会分享一个示例或链接来说明操作?

Update(s) 更新)

  1. Added a section for expected output before questions. 在问题之前添加了一段预期输出的部分。

Thanks. 谢谢。

After perusing the object browser, I discovered that the use of the FolderName property is mutually exclusive from the FolderId property, which explained why my parentFolderId.FolderName.Value code was always returning null. 仔细阅读对象浏览器后,我发现FolderName属性与FolderId属性是互斥的,这解释了为什么我的parentFolderId.FolderName.Value代码始终返回null的原因。

I reorganized my code to build the hierarchy and used the ParentFolderId property for an EmailMessage to bind to the parent folder, which allowed me to obtain the parent folder's DisplayName property. 我重新组织了代码以构建层次结构,并使用ParentFolderId属性将EmailMessage绑定到父文件夹,这使我可以获得父文件夹的DisplayName属性。

暂无
暂无

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

相关问题 在大文件夹上使用FindItems的Exchange Web Services - Exchange Web Services using FindItems on large folders Exchange Web 服务 (EWS) 在所有文件夹中查找项目 - Exchange Web Services (EWS) FindItems within All Folders 如何使用 EWS 托管 API 从文件夹和公共文件夹的子文件夹中获取所有项目 - How To Get all ITEMS from Folders and Sub-folders of PublicFolders Using EWS Managed API 从Exchange Web服务托管API获取收件箱中的所有邮件,并将其存储为.eml文件 - Fetching all mails in Inbox from Exchange Web Services Managed API and storing them as a .eml files 如何使用 C# 和 Exchange Web Services Managed API 获取文件夹的策略? - How to get a folder's policy using C# and Exchange Web Services Managed API? 如何在不使用递归的情况下列出目录中的所有文件夹和子文件夹 - How to list all the folders and sub folders in a directory without using recursion 如何在托管的Exchange Web服务中更改AppointmentStatus - How to change AppointmentStatus in managed Exchange Web Services 使用Exchange Web Services从C#中的自定义和已发送邮件文件夹获取电子邮件 - Get emails from custom and sent mail folders in c# using Exchange Web Services 使用C#中的Exchange Web服务托管API检索错误的邮箱项目 - Wrong mailbox items being retrieved using Exchange Web Services managed API in C# Folder.Bind - “Id格式错误” - Exchange Web服务托管API - Folder.Bind - “Id is malformed” - Exchange Web Services Managed API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM