Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing the GAL with a Windows program

Status
Not open for further replies.

hawaiidragon

Programmer
Aug 20, 2009
1
US
I have ran my head into about 50 diffrent walls at the momment. I am trying to access the Global Address List using a windows program. I know I am close. I just cant quite get there. Does anyone out there have any ideas?

Here is a sample of my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Outlook;
using System.Collections;


using OutLook = Microsoft.Office.Interop.Outlook;

namespace
OutlookIntegrationEx
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
//LOADING THE FODLERS IN OUT LOOK.
LoadContactFoldersCombo();
}
/// <summary>
/// loading the contact folders from outlook application
/// </summary>
private void LoadContactFoldersCombo()
{
OutLook.
_Application outlookObj = new OutLook.Application();
OutLook.
MAPIFolder contactsFolder = (OutLook.MAPIFolder)
outlookObj.Session.GetDefaultFolder(OutLook.
OlDefaultFolders.olFolderContacts);
if (!cbFolder.Items.Contains("Global Address List"))
{
cbFolder.Items.Add(
"Global Address List");
}
//VERIFYING THE CUSTOM FOLDER IN OUT LOOK .
foreach (OutLook.MAPIFolder subFolder in contactsFolder.Folders)
{
if (!cbFolder.Items.Contains(subFolder.Name))
{
cbFolder.Items.Add(subFolder.Name);
}
}
}



/// Getting the contacts by passing the folder name.
/// </summary>
/// <param name="folderName"></param>
/// <returns></returns>
private List <MyContact> GetContactsFromFolder(string folderName)
{
List<MyContact> contacts = null;
object missing = System.Reflection.Missing.Value;
//create instance of Outlook application and Outlook Contacts folder.
try
{
OutLook.MAPIFolder fldContacts = null;
contacts = new List<MyContact>();
OutLook._Application outlookObj = new OutLook.Application();
if (folderName == "Global Address List")
{
fldContacts = (OutLook.
MAPIFolder)outlookObj.Session.GetDefaultFolder(OutLook.OlDefaultFolders.olFolderContacts);
}
else
{
OutLook.
MAPIFolder contactsFolder = (OutLook.MAPIFolder)
outlookObj.Session.GetDefaultFolder(OutLook.
OlDefaultFolders.olFolderContacts);
//VERIFYING THE CUSTOM FOLDER IN OUT LOOK .
foreach (OutLook.MAPIFolder subFolder in contactsFolder.Folders)
{
if (subFolder.Name == folderName)
{
fldContacts = subFolder;
break;
}
}
}
//LOOPIN G THROUGH CONTACTS IN THAT FOLDER.
foreach (Microsoft.Office.Interop.Outlook._ContactItem contactItem in fldContacts.Items)
{
{
contact = new MyContact();
contact.FirstName = (contactItem.FirstName == null) ? string.Empty : contactItem.FirstName;
contact.LastName = (contactItem.LastName == null) ? string.Empty : contactItem.LastName;
contact.EmailAddress = contactItem.Email1Address;
contact.Phone = contactItem.Business2TelephoneNumber;
contact.Address = contactItem.BusinessAddress;
//contact.
contacts.Add(contact);
}
}
}
catch (System.Exception ex)
{
throw new ApplicationException(ex.Message);
}
return contacts;
}
/// <summary>
/// REFESHING THE CONTACTS.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRefresh_Click(object sender, EventArgs e)
{
string cntctFldrName = string.Empty;
if (cbFolder.SelectedItem == null)
{
cntctFldrName =
"Global Address List";
cbFolder.SelectedItem =
"Global Address List";
}
else
{
cntctFldrName = cbFolder.SelectedItem.ToString();
}
dgvContacts.Visible =
false;
if (GetContactsFromFolder(cntctFldrName).Count > 0)
{
dgvContacts.Visible =
true;
dgvContacts.DataSource = GetContactsFromFolder(cntctFldrName);
}
else
{
MessageBox.Show("No Contacts found in this folder");
}
}
/// <summary>
/// REFRESHING THE FOLDERS.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRefFolder_Click(object sender, EventArgs e)
{
LoadContactFoldersCombo();
}
private void dgvContacts_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top