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 a Users Contacts List from an Exchange Server

4/24/2008

A request that recently popped up at work was the ability to use a person's contacts list from outlook when they were using a particular portion of the intranet site. Not that difficult to do actually. All that is required is a call using WebDAV to the Exchange server and we have that user's contact's list. Actually there are a couple of ways to accomplish it but I found that to be the easiest. Anyway, as always I'm sure you're interested in the code:

   1: public static System.Web.UI.WebControls.ListItemCollection GetContacts(string UserName,string Password,string Directory) 
   2: {
   3:     System.Web.UI.WebControls.ListItemCollection ReturnArray = new System.Web.UI.WebControls.ListItemCollection();
   4:     string server = "http:/ /ExchangeServer";
   5:     NetworkCredential credentials = new NetworkCredential(UserName, Password);
   6:     string uri = string.Format("{0}/exchange/{1}", server, credentials.UserName);
   7:     byte[] contents = System.Text.Encoding.UTF8.GetBytes(string.Format( 
   8:  
   9:        @"<?xml version=""1.0""?> 
  10:  
  11:         <g:searchrequest xmlns:g=""DAV:""> 
  12:  
  13:             <g:sql> 
  14:  
  15:                 SELECT 
  16:  
  17:                     ""urn:schemas:contacts:givenName"", ""urn:schemas:contacts:sn"", 
  18:  
  19:                     ""urn:schemas:contacts:email1"" 
  20:  
  21:                 FROM 
  22:  
  23:                     Scope('SHALLOW TRAVERSAL OF ""{0}/exchange/{1}/contacts""') 
  24:  
  25:             </g:sql> 
  26:  
  27:         </g:searchrequest>", 
  28:  
  29:       server, credentials.UserName)); 
  30:  
  31:   
  32:  
  33:     HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;
  34:     request.Credentials = credentials;
  35:     request.Method = "SEARCH";
  36:     request.ContentLength = contents.Length;
  37:     request.ContentType = "text/xml"; 
  38:  
  39:     using (System.IO.Stream requestStream = request.GetRequestStream())
  40:         requestStream.Write(contents, 0, contents.Length); 
  41:  
  42:   
  43:  
  44:     using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
  45:     using (System.IO.Stream responseStream = response.GetResponseStream())
  46:     {
  47:         XmlDocument document = new XmlDocument();
  48:         document.Load(responseStream);
  49:         foreach (XmlElement element in document.GetElementsByTagName("a:prop"))
  50:         {
  51:             if (element["d:sn"] != null && element["d:givenName"] != null && element["d:email1"] != null)
  52:             {
  53:                 System.Web.UI.WebControls.ListItem TempItem = new System.Web.UI.WebControls.ListItem();
  54:                 TempItem.Text = element["d:sn"].InnerText+", "+element["d:givenName"].InnerText;
  55:                 TempItem.Value = element["d:email1"].InnerText;
  56:                 ReturnArray.Add(TempItem);
  57:             }
  58:         }
  59:     }
  60:     return ReturnArray;
  61: }

Note that this code is a modified version of the code you'll find here. All this call does, is makes a call doing WebDAV asking the Exchange server to search the person's contacts directory and return an item's first name, last name, and email address. We can get a lot more information if needed, you can look at the urn:schemas:contacts namespace if you want something else. Once we have the information, it just creates a ListItemCollection and drops the items in there (I was using this in a drop down list). You can of course create your own Contacts class and put the info in there if you prefer. Now if you're using Exchange 2010, this code will not work. The reason it wont work is the fact that WebDAV was dropped from Exchange 2010. Sadly I don't have access to Exchange 2010 at this moment or I'd post the bit of code needed to make it work there. Anyway, try out the code, leave feedback, and happy coding.



Comments