?
Solved

C# Search and Open contacts in outlook

Posted on 2015-02-11
6
Medium Priority
?
151 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 40605054
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
ID: 40605520
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
ID: 40605535
Ah wait i think i see it contactitem.display();
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:desiredforsome
ID: 40605580
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
ID: 40605607
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 52

Accepted Solution

by:
Rgonzo1971 earned 2000 total points
ID: 40605688
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

765 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