Other Posts in Exchange

  1. Getting Free/Busy Data from an Exchange Server
  2. Getting a Users Contacts List from an Exchange Server
  3. Getting the GAL from Exchange in C#
  4. Using WebDAV to Get Calendar Items (Appointments) from Exchange in C#/ASP.Net

Getting the GAL from Exchange in C#

5/12/2008

This is a short post for today but it does give you some rather interesting code (or at least hopefully you think so). Have you ever wanted to access your companies global address list from your Exchange server/Active Directory setup on your intranet site? Well you can, and it's actually very easy to accomplish:

   1: public static StringDictionary GetGAL(string UserName, string Password)
   2: {
   3:     try
   4:     {
   5:         StringDictionary ReturnArray = new StringDictionary();
   6:         DirectoryEntry deDirEntry = new DirectoryEntry("LDAP://server",
   7:                                                            UserName,
   8:                                                            Password,
   9:                                                            AuthenticationTypes.Secure);
  10:         DirectorySearcher mySearcher = new DirectorySearcher(deDirEntry);
  11:         mySearcher.PropertiesToLoad.Add("cn");
  12:         mySearcher.PropertiesToLoad.Add("mail");
  13:  
  14:         string sFilter = String.Format("(&(mailnickname=*)(|(objectcategory=user)(objectcategory=group)))");
  15:  
  16:         mySearcher.Filter = sFilter;
  17:         mySearcher.Sort.Direction = SortDirection.Ascending;
  18:         mySearcher.Sort.PropertyName = "cn";
  19:         mySearcher.PageSize = 1000;
  20:  
  21:         SearchResultCollection results;
  22:         results = mySearcher.FindAll();
  23:  
  24:         foreach (SearchResult resEnt in results)
  25:         {
  26:             ResultPropertyCollection propcoll = resEnt.Properties;
  27:             string Name = "";
  28:             string Mail = "";
  29:             foreach (string key in propcoll.PropertyNames)
  30:             {
  31:                 if (key == "cn")
  32:                 {
  33:                     foreach (object values in propcoll[key])
  34:                     {
  35:                         Name = values.ToString();
  36:                     }
  37:                 }
  38:                 else if (key == "mail")
  39:                 {
  40:                     foreach (object values in propcoll[key])
  41:                     {
  42:                         Mail = values.ToString();
  43:                     }
  44:                 }
  45:             }
  46:             if (!string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Mail))
  47:             {
  48:                 ReturnArray.Add(Name, Mail);
  49:             }
  50:         }
  51:         return ReturnArray;
  52:     }
  53:     catch
  54:     {
  55:         return null;
  56:     }
  57: }

That's all there is to it. That code logs into your Active Directory (note that you have to change the code to actually point to your server) using whatever username and password you supply. It then makes a query, asking for the user name and email address of every group and individual that also has a mail nickname, sorting that list alphabetically by the user name. It then puts the returned values into a StringDictionary. So really you're querying AD and not Exchange but it's the same data as Exchange simply uses AD to store the information. Anyway, take a look at the code, leave feedback, and as always happy coding.



Comments