SIGN UP MEMBER LOGIN:    
ARTICLE

Accessing Outlook contacts list in C#

Posted by Hirendra Sisodiya Articles | Office Development April 26, 2010
This article show how to access Outlook contact list.
Reader Level:

This article show how to access Outlook Contact list

For better understanding we take an example. In this example there is one window form with datagridview. We have to show all contacts in this datagridview, something like this:

Step1.JPG

Start working with  Microsoft.Office.Interop.Outlook:

Add referance  of Microsoft.Office.Interop.Outlook library(example is using Microsoft.Office.Interop.Outlook 12.0)

Add one DatagridView and one button on form1.

And declare:

Microsoft.Office.Interop.Outlook.Items OutlookItems;

Microsoft.Office.Interop.Outlook.Application outlookObj;

MAPIFolder Folder_Contacts;

 

Work on Form1:


private void Form1_Load(object sender, EventArgs e)

{

    Microsoft.Office.Interop.Outlook.Application outlookObj = new Microsoft.Office.Interop.Outlook.Application();

    MAPIFolder Folder_Contacts = (MAPIFolder)outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts); 

    OutlookItems = Folder_Contacts.Items; 

    for (int i = 0; i < oItems.Count; i++)

    {

        Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)OutlookItems[i + 1];

        dataGridView1.Rows.Add();

        if (contact.FirstName != null)

        {

            dataGridView1.Rows[i].Cells[0].Value = contact.FirstName;

        }

        else

        {

            dataGridView1.Rows[i].Cells[0].Value = "";

        }

        if (contact.MiddleName != null)

        {

            dataGridView1.Rows[i].Cells[1].Value = contact.MiddleName;

        }

        else

        {

            dataGridView1.Rows[i].Cells[1].Value = "";

        }

        if (contact.LastName != null)

        {

            dataGridView1.Rows[i].Cells[2].Value = contact.LastName;

        }

        else

        {

            dataGridView1.Rows[i].Cells[2].Value = "";

        }

        if (contact.Email1Address != null)

        {

            dataGridView1.Rows[i].Cells[3].Value = contact.Email1Address;

        }

        else

        {

            dataGridView1.Rows[i].Cells[3].Value = "";

        }

        }

}


Add code for showing contact'description on new form frmEditContact :

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)

{

    if (e.RowIndex != -1)

    {

        Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)OutlookItems[e.RowIndex + 1];

        frmEditContact frm = new frmEditContact();

        frm.Fname = contact.FirstName;

        frm.Mname = contact.MiddleName;

        frm.Lname = contact.LastName;

        frm.Eamil = contact.Email1Address;

        frm.ShowDialog();

    }

}

Work on frmEditContact:

public partial class frmEditContact : Form

{

    public frmEditContact()

    {

        InitializeComponent();

    }

    public string Fname = "";

    public string Mname = "";

    public string Lname = "";

    public string Eamil = "";

    private void frmEditContact_Load(object sender, EventArgs e)

    {

        txtFname.Text = Fname;

        txtMname.Text = Mname;

        txtLname.Text = Lname;

        txtEmail.Text = Eamil;

    }

}

frmEditContact will show as:

Step2.JPG

Login to add your contents and source code to this article
share this article :
post comment
 

The above code not get list of contacts in outlook 2010

Posted by Sunil Goud Sep 07, 2011

Is there any way to find out the list of delegated users from active directory using C# i.e., who are delegated using outlook/exchange account. Thanks in advance...

Posted by Anilkumar Manthena Mar 14, 2011

How to read other contacts, like a public contacts? (exchange)

Posted by Rafal Lukowiak Mar 03, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor