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.
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.
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. 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. 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.
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.
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.
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.