Search code examples
c#outlookcontacts

Searching public Outlook contacts folder from C#


We have a large public contacts folder in Outlook called Global Contacts, I'd like to be able to search through it and return a number of results that match certain criteria, ideally wildcard-style.

E.g. if someone puts "je" in the 'name' textbox, it will return all contacts whose names contain 'je'. This may be coupled as an AND with a companyname textbox.

Most of the examples I've seen are either in VB, or are concerned with doing this form a web app - I'm doing a winforms app, and every machine has Outlook 2002 installed (yeah, I know, update long overdue).

Can anyone point me in the right direction? Some code would be nice as a place to start.

Cheers


Solution

  • I ended up doing this:

                Microsoft.Office.Interop.Outlook._Application objOutlook; //declare Outlook application
                objOutlook = new Microsoft.Office.Interop.Outlook.Application(); //create it
                Microsoft.Office.Interop.Outlook._NameSpace objNS = objOutlook.Session; //create new session
                Microsoft.Office.Interop.Outlook.MAPIFolder oAllPublicFolders; //what it says on the tin
                Microsoft.Office.Interop.Outlook.MAPIFolder oPublicFolders; // as above
                Microsoft.Office.Interop.Outlook.MAPIFolder objContacts; //as above
                Microsoft.Office.Interop.Outlook.Items itmsFiltered; //the filtered items list
                oPublicFolders = objNS.Folders["Public Folders"];
                oAllPublicFolders = oPublicFolders.Folders["All Public Folders"];
                objContacts = oAllPublicFolders.Folders["Global Contacts"];
    
                itmsFiltered = objContacts.Items.Restrict(strFilter);//restrict the search to our filter terms
    

    Then just looping through itmsFiltered to add it to an ObjectListView. Hopefully this will be of use to someone else looking to do the same - it took me a while to cobble this together from various sources.