I am implementing adapter using Microsoft.Exchange.WebServices 2.2.0. I am stuck on:
The scenario: fetching all contacts (even those created/moved outside the Contacts folder (WellKnownFolderName.Contacts)).
List<Contact> result = new List<Contact>();
FindFoldersResults allFolders = _service.FindFolders(WellKnownFolderName.Root, new FolderView(int.MaxValue) { Traversal = FolderTraversal.Deep }); // Getting all folders
foreach (var folder in allFolders) // loop thru all folders
{
SearchFilter.IsEqualTo contactSchemaFilter = new SearchFilter.IsEqualTo(ItemSchema.ItemClass, "Microsoft.Exchange.WebServices.Data.ContactSchema");
FindItemsResults<Item> discoveredContactsInFolder = _service.FindItems(folder.Id, contactSchemaFilter, new ItemView(int.MaxValue)); // find all items which has same Schema as Contacts.
result = discoveredContactsInFolder.Select(c => c as Contact).ToList();
}
My problem: Beside the Contacts that I am looking for, I am getting the Global Address List and Contact Groups.
Q: Am I missing any filter or completely different approach?
Best regards, SVG
Firstly with your code you should be paging https://msdn.microsoft.com/en-us/library/office/dn592093(v=exchg.150).aspx both FindFolder and FindItems the problem with your sample is if you have more the 1000 folders or 1000 contacts because of the way throttling works with Exchange it won't process
SearchFilter.IsEqualTo contactSchemaFilter = new SearchFilter.IsEqualTo(ItemSchema.ItemClass, "Microsoft.Exchange.WebServices.Data.ContactSchema");
The ItemClass property is the ContentClass https://msdn.microsoft.com/en-us/library/aa125194(v=exchg.65).aspx so for Contacts this would be
IPM.Contact
Contact Groups have a ItemClass of IPM.DistList
FindFoldersResults allFolders = _service.FindFolders(WellKnownFolderName.Root, new FolderView(int.MaxValue) { Traversal = FolderTraversal.Deep }); // Getting all folders
I would suggest you start your search at MsgFolderRoot the problem with using Root is this takes into account the NON_IPM_Subtree in the Mailbox which includes all the system folders so would contain a lot of cached (eg GAL entries etc). Because the NON_IPM_Subtree isn't visable to users there should be no user created contacts in those folders.