Searching Global Address List/Book
Asked Answered
A

3

6

I'm developing an app that will allow the user user to view the contents of an inbox that they have access to. I am having a difficult time trying to find a means of searching the Global Address List other then

AddressEntries entries = global.AddressEntries;
AddressEntry entry = entries["search value"];

This works but only returns one instance, and it is the first one found. I basically want to provide a list to the user if there are multiple results.

Secondly I would like to be able to view the contact details, but when I use the

ContactItem contact = entry.GetContact();

It always returns null, I think that it is because it is looking at the current user's contact personal list

I guess that I am trying to create a simple version of the Global Address Book window in Outlook, if that makes sense.

Anyway if anyone has any ideas or references I would be grateful!

Thanks Patrick

Aggappera answered 18/11, 2010 at 1:0 Comment(0)
T
6

You should be able to get the Global Address List from current profile as shown below.

Outlook.AddressLists addrLists = Application.Session.AddressLists;
Outlook.AddressList gal = addrLists["Global Address List"];

Then you can enumerate and display the members of that AddressList.

There's another way to do this described on MSDN here.

How to: Enumerate the Entries in the Global Address List

Tense answered 18/11, 2010 at 1:19 Comment(4)
Hi, thanks for the tip. I got it to work, well sort of. I almost get through 10k entries and then an error is thrown, not sure what it is because it doesn't say. I know there has to be a better way to query the GAL then enumerating through the entire list (of almost 90k)Aggappera
@Aggappera - some kind of cursor or bookmark API would be useful to allow processing in chunks - don't know how Outlook does this so quickly. As you noted, not this way for sure.Tense
I have an error Application does not contain a definition for Session. How do I do in this case?Capriola
The MSDN example code is still live so I guess that you either did not include the Application class, or are picking up an incorrect one?Tense
W
5
string[] names;
Outlook.AddressLists addrLists = Application.Session.AddressLists; 
Outlook.AddressList gal = addrLists["Global Address List"];

//for a distrubution list do this...
Outlook.AddressEntry entry = gal.AddressEntries["distribution list"];
Outlook.ExchangeDistributionList exchDL = entry.GetExchangeDistributionList();
Outlook.AddressEntries addrEntries = exchDL.GetExchangeDistributionListMembers();

names = new string[addrEntries.Count];

for (int i = 0; i < addrEntries.Count; i++)
{
    Outlook.AddressEntry exchDLMember = addrEntries[i];
    names[i] = exchDLMember.Name;
}

return names;

//for an individual you could do something like this...
Outlook.AddressEntry entry = gal.AddressEntries["contact nickname"];

Outlook.ContactItem contact = entry.GetContact();
string name = contact.NickName;
string email = contact.Email1Address;
Wideangle answered 8/2, 2012 at 16:4 Comment(0)
E
0
  1. Install Outlook Interop package Install-Package Microsoft.Office.Interop.Outlook
  2. Here's the code (taken from MSDN)
using Microsoft.Office.Interop.Outlook;

var application = new Application();
            AddressList gal = application.Session.GetGlobalAddressList();
            if (gal != null)
            {
                for (int i = 1; i < gal.AddressEntries.Count - 1; i++)
                {
                    AddressEntry addrEntry = gal.AddressEntries[i];

                    if (addrEntry.AddressEntryUserType == OlAddressEntryUserType.olExchangeUserAddressEntry || 
                        addrEntry.AddressEntryUserType == OlAddressEntryUserType.olExchangeRemoteUserAddressEntry)
                    {
                        ExchangeUser exchUser = addrEntry.GetExchangeUser();
                        Console.WriteLine($"{ exchUser.Name } { exchUser.PrimarySmtpAddress }");
                    }
                    if (addrEntry.AddressEntryUserType == OlAddressEntryUserType.olExchangeDistributionListAddressEntry)
                    {
                        ExchangeDistributionList exchDL = addrEntry.GetExchangeDistributionList();
                        Console.WriteLine($"{ exchDL.Name } { exchDL.PrimarySmtpAddress }");
                    }
                }
            }
Ensample answered 12/4, 2021 at 12:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.