5

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

DjCzermino
  • 125
  • 1
  • 9

3 Answers3

3

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);
Josh
  • 68,005
  • 14
  • 144
  • 156
  • Glad to hear. If you found the answer helpful, please vote it up and mark it as answered so the question doesn't remain open. – Josh Jan 05 '11 at 00:06
2

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);
DjCzermino
  • 125
  • 1
  • 9
1

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

Eric Legault
  • 5,706
  • 2
  • 22
  • 38