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
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
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.