VSTO Outlook - Contact iteration is SO SLOW!
I'm working on an outlook add-in and I have a dialog window that allows the user to select contacts. I havent been able to find a way to use the outlook contact window so I am looping through the ContactFolder.Items and doing my work that way.
The problem is that I have to handle up to 70K contacts. I tried multi-threading and many other things but it is just so slow. It takes 15 seconds to load 30k contacts.
I can lo开发者_StackOverflow中文版ad and bind 500k POCO objects in milliseconds but when I need to get the contact items from outlook it just takes forever. The problem seems to be when you actually need to get a property from the contactitem it has to fetch it from the database or something. Is there a contact cache I can pull from? I only need Display and Email, nothing else. An ID would be nice but I don't need it.
Can someone please tell me a better way of getting contacts from outlook or at least tell me how to open the outlook contact selection window? I was able to find code to open it but it wont let me because I'm showing a modal dialog and it wont open if there is a modal open.
Answer:
Microsoft.Office.Interop.Outlook.NameSpace ns = Globals.ThisAddIn.Application.GetNamespace("MAPI");
Microsoft.Office.Interop.Outlook.MAPIFolder contactsFld = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);
Microsoft.Office.Interop.Outlook.Table tb = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts).GetTable(null, Microsoft.Office.Interop.Outlook.OlItemType.olContactItem);
tb.Columns.RemoveAll();
tb.Columns.Add("Email1Address");
tb.Columns.Add("FullName");
object[,] otb = tb.GetArray(100000) as object[,];
int len = otb.GetUpperBound(0);
for (int i = 0; i < len; i++)
{
if (otb[i, 0] == null)
{
continue;
}
Contacts.Add(new ContactItem() { ContactDisplay = otb[i, 1].ToString(), ContactEmail = otb[i, 0].ToString() });
}
This loads in less than a second which is fast enough to put it back on the UI thread.
精彩评论