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

July 17th, 2010 § 13 comments

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
    http://Your_exchange_server_name/EWS/Services.wsdl.

    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.
    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");
            }
        }
    }
    

What's this?

You are currently reading Querying the Global Address List (GAL) via Exchange Web Services (EWS) at A Low Man's Lyric.

meta