Querying the Global Address List (GAL) via Exchange Web Services (EWS)

If you are looking for a way to search the Global Address List (GAL) for a particular contact via Exchange Web Services, read on. Please note that this only works for Exchange 2007 and above.

  1. Grab the WSDL and generate stubs. The normal location for the WSDL is
    [sourcecode language=”html”]http://Your_exchange_server_name/EWS/Services.wsdl.[/sourcecode]

    Note that when you plug this URL into your browser, you might be directed to login. Once you login with your credentials, you will be redirected to your regular email page. Despair not. Enter the URL in your address bar again and you should have the WSDL.

  2. Use the code below to query for a user with a name “test”. The code below is in C#, but should translate quite easily to other languages. Also take note of the URL. In my case, I needed to use https. Also note that the username is not the email but just the prefix i.e. the @domain.com is not required.
    [sourcecode language=”csharp”]
    static void Main(string[] args)
    {
    ExchangeServiceBinding esb = new ExchangeServiceBinding();
    esb.Url = @"https://myserver/EWS/Exchange.asmx";
    esb.Credentials = new NetworkCredential(
    "username",
    @"password",
    @"domain");

    // Create the ResolveNamesType and set
    // the unresolved entry.
    ResolveNamesType rnType = new ResolveNamesType();
    rnType.ReturnFullContactData = true;
    rnType.UnresolvedEntry = "test";

    // Resolve names.
    ResolveNamesResponseType resolveNamesResponse
    = esb.ResolveNames(rnType);
    ArrayOfResponseMessagesType responses
    = resolveNamesResponse.ResponseMessages;

    // Check the result.
    if (responses.Items.Length > 0 &&
    responses.Items[0].ResponseClass
    != ResponseClassType.Error)
    {
    ResolveNamesResponseMessageType responseMessage =
    responses.Items[0] as
    ResolveNamesResponseMessageType;

    // Display the resolution information.
    ResolutionType[] resolutions =
    responseMessage.ResolutionSet.Resolution;
    foreach (ResolutionType resolution
    in resolutions)
    {
    Console.WriteLine(
    "Name: " +
    resolution.Contact.DisplayName
    );
    Console.WriteLine(
    "EmailAddress: " +
    resolution.Mailbox.EmailAddress
    );

    if (resolution.Contact.PhoneNumbers != null)
    {
    foreach (
    PhoneNumberDictionaryEntryType phone
    in resolution.Contact.PhoneNumbers)
    {
    Console.WriteLine(
    phone.Key.ToString() +
    " : " +
    phone.Value
    );
    }
    }
    Console.WriteLine(
    "Office location:" +
    resolution.Contact.OfficeLocation
    );
    Console.WriteLine("\n");
    }
    }
    }
    [/sourcecode]

14 thoughts on “Querying the Global Address List (GAL) via Exchange Web Services (EWS)”

  1. Excellent! Exactly what I needed! Sample code was good, though it needed error handling because I had “contact” with a null DisplayName but a valid mailbox.

  2. Are these two resolution.Contact.DisplayName, and
    resolution.Mailbox.EmailAddress could get null exception ? Our GAL contains Multi-Language name,could that be the problems? I add if(resolution.Mailbox.DisplayName != null) checking and error goes to that checking line? I do not see the buffer over 100 entries. By any chance anyone can provide some information?

  3. This is just amazing. above code get accounts which as test name in it. How to get all active user accounts ? Thanks in advance

  4. I’m using ExchangeVersion.Exchange2010_SP1 and I get:
    ResolveNamesType is not defined.

    This worked for me in VB.Net:

    For Each nr As NameResolution In esb.ResolveName(txtUserId.Text, ResolveNameSearchLocation.DirectoryOnly, False)
    lblResult.Text &= "
    " & nr.Mailbox.Name & " - " & nr.Mailbox.Address
    'lblResult.Text &= "
    " & nr.Contact.DisplayName & " - " & nr.Contact.EmailAddresses(EmailAddressKey.EmailAddress1).Address
    Next

  5. This code works like charm. But need one help. When I execute “resolution.Contact.DirectReports” it fetches me NULL value for all the managers. But when I check it over exchange there are 11 direct reports configured.
    Please help

  6. Hi, Can we get direct reports user list by using this solution. Currently I am trying to get the list of DirectReports but it is showing null every time.

Leave a Reply

Your email address will not be published. Required fields are marked *