Solved

C# Search and Open contacts in outlook

Posted on 2015-02-11
6
93 Views
Last Modified: 2015-03-17
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.
0
Comment
Question by:desiredforsome
  • 4
  • 2
6 Comments
 
LVL 48

Expert Comment

by:Rgonzo1971
Comment Utility
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
0
 

Author Comment

by:desiredforsome
Comment Utility
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.
0
 

Author Comment

by:desiredforsome
Comment Utility
Ah wait i think i see it contactitem.display();
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:desiredforsome
Comment Utility
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");
                    }
                   
                }
               
               
             
               
            }
0
 

Author Comment

by:desiredforsome
Comment Utility
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

0
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
pls try

if(found ==0)
               {
                Outlook.ContactItem newContact = (Outlook.ContactItem)
                outlookApplication.CreateItem(Outlook.OlItemType.olContactItem);
                try
                {
                    newContact.FirstName = "Jo";
                    newContact.LastName = "Berry";
                    newContact.Email1Address = "somebody@example.com";
                    newContact.CustomerID = "123456";
                    newContact.PrimaryTelephoneNumber = "(425)555-0111";
                    newContact.MailingAddressStreet = "123 Main St.";
                    newContact.MailingAddressCity = "Redmond";
                    newContact.MailingAddressState = "WA";
                    newContact.Save();
                    newContact.Display(true);
                }
                catch
                {
                    Console.WriteLine("The new contact was not saved.");
                }
}
                found = 0;

Open in new window

EDIT closing If bracket
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now