[英]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 问题
Update(s) 更新)
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.