Link to home
Start Free TrialLog in
Avatar of desiredforsome
desiredforsome

asked on

C# Search and Open contacts in outlook

I am coding an application to integrate with our phone system. However, I am having issues finding any documentation on how to search the current outlook profile that is open and if it finds the match for a phone number it pulls open the contact in outlook. If it does not find the match it open a new contact to create.

Looking for some pointers on this. I know it can be done I jsut cant figure it out in the SDK.
Avatar of Rgonzo1971
Rgonzo1971

HI,

to read the contacts for their numbers try

using System;
using Outlook = Microsoft.Office.Interop.Outlook;
class Program
{

    static void Main()
    {
        var outlookApplication = new Microsoft.Office.Interop.Outlook.Application();

        Outlook.NameSpace mapiNamespace = outlookApplication.GetNamespace("MAPI");
        Outlook.MAPIFolder contacts = mapiNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);

        foreach (Outlook.ContactItem foundContact in contacts.Items)
        {
            if (foundContact.BusinessTelephoneNumber != null)
            {
                Console.WriteLine(foundContact.FullName);
                Console.WriteLine(foundContact.BusinessTelephoneNumber);
                Console.WriteLine();
            }
        }
   }
}

Open in new window

Regards
Avatar of desiredforsome

ASKER

I know that I can read the contacts this way but I want it to popup the contact file within outlook when it finds it.
Ah wait i think i see it contactitem.display();
So I got part working. I got it to pull up the contact if it exists but I cant get ti to pull up a blank new contact if it does not exist. I even tried testing it with just having a message box show up.

It seems to be ignoring my else

        if(e.Channel.ToUpper().StartsWith("SIP/200"))
            {
               
                astTrayNotify.Icon = SystemIcons.Application;
                astTrayNotify.BalloonTipTitle = "Incomming Call";
                astTrayNotify.BalloonTipText = e.CallerIdName.ToString() + Environment.NewLine + e.CallerIdNum.ToString();
                astTrayNotify.ShowBalloonTip(3000);
                var outlookapplication = new Microsoft.Office.Interop.Outlook.Application();
                Outlook.NameSpace mapiNamespace = outlookapplication.GetNamespace("MAPI");
             Outlook.ContactItem contactitem =  (Outlook.ContactItem)outlookapplication.CreateItem(Outlook.OlItemType.olContactItem);
             Outlook.MAPIFolder contacts = mapiNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
           
                foreach (Outlook.ContactItem foundContact in contacts.Items)
                {
                    if (foundContact.BusinessTelephoneNumber == e.CallerIdNum.ToString())
                    {
                       
                        foundContact.Display();
                    }
                    else
                    {
                        MessageBox.Show("TEST");
                    }
                   
                }
               
               
             
               
            }
I have it working with the following code although I dont belive this is the clean way of doing things.

      if(e.Channel.ToUpper().StartsWith("SIP/200"))
            {
                
                astTrayNotify.Icon = SystemIcons.Application;
                astTrayNotify.BalloonTipTitle = "Incomming Call";
                astTrayNotify.BalloonTipText = e.CallerIdName.ToString() + Environment.NewLine + e.CallerIdNum.ToString();
                astTrayNotify.ShowBalloonTip(3000);
                var outlookapplication = new Microsoft.Office.Interop.Outlook.Application();
                Outlook.NameSpace mapiNamespace = outlookapplication.GetNamespace("MAPI");
             Outlook.ContactItem contactitem =  (Outlook.ContactItem)outlookapplication.CreateItem(Outlook.OlItemType.olContactItem);
             Outlook.MAPIFolder contacts = mapiNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
            
                foreach (Outlook.ContactItem foundContact in contacts.Items)
                {
                    if (foundContact.BusinessTelephoneNumber == e.CallerIdNum.ToString())
                    {
                        found = 1;
                        foundContact.Display();
                        
                    }
                    
                    
                }
               if(found ==0)
               {
                   contactitem.FullName = e.CallerIdName.ToString();
                   contactitem.BusinessTelephoneNumber = e.CallerIdNum.ToString();
                   contactitem.Display();
                   found = 0;

               }
                if(found==1)
                {
                    found = 0;
                }
               
             
                
            }
     

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial