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:
public static System.Web.UI.WebControls.ListItemCollection GetContacts(string UserName,string Password,string Directory)
{
System.Web.UI.WebControls.ListItemCollection ReturnArray = new System.Web.UI.WebControls.ListItemCollection();
string server = "http:/ /ExchangeServer";
NetworkCredential credentials = new NetworkCredential(UserName, Password);
string uri = string.Format("{0}/exchange/{1}", server, credentials.UserName);
byte[] contents = System.Text.Encoding.UTF8.GetBytes(string.Format(
@"<?xml version=""1.0""?>
<g:searchrequest xmlns:g=""DAV:"">
<g:sql>
SELECT
""urn:schemas:contacts:givenName"", ""urn:schemas:contacts:sn"",
""urn:schemas:contacts:email1""
FROM
Scope('SHALLOW TRAVERSAL OF ""{0}/exchange/{1}/contacts""')
</g:sql>
</g:searchrequest>",
server, credentials.UserName));
HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;
request.Credentials = credentials;
request.Method = "SEARCH";
request.ContentLength = contents.Length;
request.ContentType = "text/xml";
using (System.IO.Stream requestStream = request.GetRequestStream())
requestStream.Write(contents, 0, contents.Length);
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
using (System.IO.Stream responseStream = response.GetResponseStream())
{
XmlDocument document = new XmlDocument();
document.Load(responseStream);
foreach (XmlElement element in document.GetElementsByTagName("a:prop"))
{
if (element["d:sn"] != null && element["d:givenName"] != null && element["d:email1"] != null)
{
System.Web.UI.WebControls.ListItem TempItem = new System.Web.UI.WebControls.ListItem();
TempItem.Text = element["d:sn"].InnerText+", "+element["d:givenName"].InnerText;
TempItem.Value = element["d:email1"].InnerText;
ReturnArray.Add(TempItem);
}
}
}
return ReturnArray;
}
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. Anyway, use the code, leave feedback, and happy coding.
dd049fc7-035d-49fa-8a2f-bdf4ca3bda9a|0|.0