5
votes

How to open Contact using C# VSTO Outlook 2007 addin by EntryID.

Now I am foreaching all contacts in Contact Folder:

string entryid = ...

Outlook.Application outlookApp = new Outlook.Application();
Outlook.MAPIFolder fldContacts = outlookApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts) as Outlook.MAPIFolder;
foreach (Outlook._ContactItem contact in fldContacts.Items)
{
    if (contact.EntryID==entryid) {
         contact.Display(false);
         break;
    }
}

but this is not effective code for many contacts in Contact Folder

3

3 Answers

3
votes

You want to use the GetItemFromID method of the NameSpace object (unintuitively, this can be accessed via the Application.Session property as you're doing above.)

You will need the Store ID of the MAPI store from which you want to retrieve the item. This can be most easily retrieved from the Folder object which you've also already got a reference to.

string entryid = ...

var outlookApp = new Outlook.Application();
var outlookNS = outlookApp.Session;
var fldContacts = outlookNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
var contact = outlookNS.GetItemFromID(entryid, fldContacts.StoreID);
2
votes

final code:

var outlookNS = this.Application.Session;
var fldContacts = outlookNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
ContactItem contact = (ContactItem)outlookNS.GetItemFromID(entryid, fldContacts.StoreID);
contact.Display(false);
1
votes

I'd recommend using the Folder.GetTable method for performant enumeration of a large volume of items:

http://msdn.microsoft.com/en-us/library/bb147574(office.12).aspx