title: query an exchange distribution list
body: <p>I'm trying to find some code that I can use in vb.net 4.0 to query the our exchange 2013 server.  It will be housed on a web server and that server does not have outlook installed on it.  Looks like I need to use EWS to do this but I've tried a lot of code snippets and still have not been able to figure this out.  The distribution list i'm trying to query is in the public folders/Office Contacts.  I've tried examples that use nesting to go through the public folder seen there is no deep traversal but I'm not doing something right there.  I am not posting code because i'm not sure it would help.  I was hoping someone has already done this and would give me some nuggest of info to get me started.</p>
  
  <p>The examples I've found do not query the distribution list but rather add to it.  It's not that I haven't tried... I've got hundreds of lines of code from different places that I've tried and tried to learn from.. but i'm not getting it done.  Anyway.. help would be great.</p>
  
  <p>Sorry about not posting any code.. I actually thought I deleted this post.. but i'll post the code that is now working for me.  This code does a query to the public folder and then grabs some of the data about each contact in that contact list.</p>
  
  <pre><code>Public Sub MS()
      Dim oTheListS As New List(Of TheList)
      Dim service As New ExchangeService(ExchangeVersion.Exchange2010_SP1)
      service.Credentials = New WebCredentials("userid", "password")
      service.AutodiscoverUrl("email@address")
  
      'Get Public Folder
      Dim sf As SearchFilter = New SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Office Contacts")
      Dim rrRes As FindFoldersResults = service.FindFolders(WellKnownFolderName.PublicFoldersRoot, sf, New FolderView(1))
      Dim OfficeContacts As Folder = rrRes.Folders(0)
      'Find the Distribution List
      Dim dlSearch As SearchFilter = New SearchFilter.IsEqualTo(ContactGroupSchema.DisplayName, "Merit Board")
      Dim ivItemView As New ItemView(1)
      Dim fiResults As FindItemsResults(Of Item) = OfficeContacts.FindItems(dlSearch, ivItemView)
      If fiResults.Items.Count = 1 Then
          'Enumeate Members
          Dim cg As ContactGroup = DirectCast(fiResults.Items(0), ContactGroup)
          cg.Load()
          For Each gm As GroupMember In cg.Members
              Dim o As New TheList
              o = MS2(gm.AddressInformation.Address)
              oTheListS.Add(o)
              'Dim o As New TheList
  
              'Dim ncCol As NameResolutionCollection = service.ResolveName(gm.AddressInformation.Address, ResolveNameSearchLocation.ContactsOnly, True)
  
  
  
              'With o
              '    .Name = gm.AddressInformation.Name
              '    .Email = gm.AddressInformation.Address
              'End With
              'oTheListS.Add(o)
  
          Next
      End If
  
  End Sub
  
   Public Function MS2(pEmail As String) As TheList
      Dim o As New TheList
      Dim service As New ExchangeService(ExchangeVersion.Exchange2010_SP1)
      service.Credentials = New WebCredentials("userid", "password")
      service.AutodiscoverUrl("email@address")
  
  
  
      Dim sf As SearchFilter = New SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Office Contacts")
      Dim rrRes As FindFoldersResults = service.FindFolders(WellKnownFolderName.PublicFoldersRoot, sf, New FolderView(1))
      Dim OfficeContacts As Folder = rrRes.Folders(0)
      'Find the Distribution List
      Dim dlSearch As SearchFilter = New SearchFilter.IsEqualTo(ContactSchema.EmailAddress1, pEmail)
      Dim ivItemView As New ItemView(1)
      Dim fiResults As FindItemsResults(Of Item) = OfficeContacts.FindItems(dlSearch, ivItemView)
      If fiResults.Items.Count = 1 Then
  
          Dim con As Contact = fiResults.Items(0)
  
          'Dim ncCol As NameResolutionCollection = service.ResolveName(gm.AddressInformation.Address, ResolveNameSearchLocation.ContactsOnly, True)
  
          With o
              If con.DisplayName IsNot Nothing Then
                  .Name = con.DisplayName
              End If
              Dim em As New EmailAddress
              If con.EmailAddresses.TryGetValue(EmailAddressKey.EmailAddress1, em) = True Then
                  .Email = con.EmailAddresses(EmailAddressKey.EmailAddress1).ToString
              End If
              If con.JobTitle IsNot Nothing Then
                  .Title = con.JobTitle
              End If
              Dim phy As New PhysicalAddressEntry
              If con.PhysicalAddresses.TryGetValue(PhysicalAddressKey.Business, phy) = True Then
                  .Address = con.PhysicalAddresses(PhysicalAddressKey.Business)
              End If
  
              If con.PhoneNumbers.TryGetValue(PhoneNumberKey.BusinessPhone, String.Empty) = True Then
                  .PhoneBusiness = con.PhoneNumbers(PhoneNumberKey.BusinessPhone)
              End If
              If con.PhoneNumbers.TryGetValue(PhoneNumberKey.MobilePhone, String.Empty) = True Then
                  .PhoneMobile = con.PhoneNumbers(PhoneNumberKey.MobilePhone)
              End If
              If con.CompanyName IsNot Nothing Then
                  .Comapny = con.CompanyName
              End If
  
  
          End With
      End If
      Return o
  End Function
  Public Class TheList
      Public Property Name As String
      Public Property Email As String
      Public Property PhoneMobile As String
      Public Property PhoneBusiness As String
      Public Property Comapny As String
      Public Property Title As String
      Public Property Address As PhysicalAddressEntry
  
  
  
  End Class
  </code></pre>
  
  <p>I just got it working so I haven't started to refine it yet.. but hopefully this will help someone else as I didn't find any code that did this</p>