Using Name Resolution (Exchange Web Services)

Topic Last Modified: 2007-09-17

You can use Exchange Web Services to resolve names against the Active Directory directory service and a user's contact folder.

Example

The following example shows you how to resolve an ambiguous name. This example shows two possible resolutions for the given name.

static void Main()
{
    // Create the bindings and set the credentials.
    ExchangeServiceBinding esb = new ExchangeServiceBinding();
    esb.Url = "http://myExchangeServer/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);

    // Check the result.
    if (resolveNamesResponse.ResponseMessages.Items.Length > 0 &&
        resolveNamesResponse.ResponseMessages.Items[0].ResponseClass != ResponseClassType.Error)
    {
        ResolveNamesResponseMessageType responseMessage = resolveNamesResponse.ResponseMessages.Items[0] as ResolveNamesResponseMessageType;
        // Display the resolution information.
        foreach (ResolutionType resolutionType in responseMessage.ResolutionSet.Resolution)
        {
            Console.WriteLine("EmailAddress: " + resolutionType.Mailbox.EmailAddress);
            Console.WriteLine("Name: " + resolutionType.Mailbox.Name);
            Console.WriteLine("MailboxType: " + resolutionType.Mailbox.MailboxType.ToString());
            Console.WriteLine("RoutingType: " + resolutionType.Mailbox.RoutingType);
            if (resolutionType.Mailbox.ItemId != null)
            {
               Console.WriteLine("ItemId Id:" + resolutionType.Mailbox.ItemId.Id);
               Console.WriteLine("ItemId ChangeKey:" + resolutionType.Mailbox.ItemId.ChangeKey);
            }
        }
    }
}

The following XML example shows the XML request message that is sent from the client to the server.

<?xml version="1.0" encoding="utf-8"?>
<ResolveNames xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                  ReturnFullContactData="true">
      <UnresolvedEntry xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">Test</UnresolvedEntry>
</ResolveNames>

The following XML example shows the XML response message that is sent from the server to the client.

<?xml version="1.0" encoding="utf-8"?>
<ResolveNamesResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                          xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ResponseMessages xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
    <ResolveNamesResponseMessage ResponseClass="Success">
      <ResponseCode>NoError</ResponseCode>
      <ResolutionSet IncludesLastItemInRange="true" 
                     TotalItemsInView="1">
        <Resolution xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
          <Mailbox>
            <Name>TestDL</Name>
            <EmailAddress>TestDL@myDomain.com</EmailAddress>
            <RoutingType>SMTP</RoutingType>
            <MailboxType>PublicDL</MailboxType>
          </Mailbox>
          <Contact>
            <DisplayName>TestDL</DisplayName>
            <EmailAddresses>
              <Entry Key="EmailAddress1">SMTP:TestDL@myDomain.com</Entry>
            </EmailAddresses>
            <ContactSource>ActiveDirectory</ContactSource>
          </Contact>
        </Resolution>
      </ResolutionSet>
    </ResolveNamesResponseMessage>
  </ResponseMessages>
</ResolveNamesResponse>

The SOAP messages that are passed between the Exchange Web Services client and server are defined by the XML schema and WSDL files. The XML schema and WSDL files define the contract between the client and server. Proxy class generators create an object-model abstraction of those SOAP messages, which can simplify programming. This code example uses a proxy class library that was generated by Microsoft Visual Studio 2005. Different proxy class generators create different object models for a given Web service. This proxy class code example is an illustration only. Refer to the proxy class generator documentation for support for proxy classes.

Compiling the Code

For information about compiling the code, see Exchange Web Services Client Development.

Robust Programming

For information about robust programming, see Exchange Web Services Architecture.