Solved

C# Search and Open contacts in outlook

Posted on 2015-02-11
6
116 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 50

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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

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 50

Accepted Solution

by:
Rgonzo1971 earned 500 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

Independent Software Vendors: 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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

761 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