using System;
using System.Text;
using System.IO;
using System.Net;
using System.Xml;
public static void PrintContactsUsingExchangeWebDAV(string Folders)
{
int i=0;
string server = "http://192.168.XXX.XXX";
string User = "name";
string pw = "pass";
string
dom = "domain";
NetworkCredential credentials = new NetworkCredential(User, pw,
dom);
string
uri = string.Format("{0}/public/{1}", server, Folders);
// Create a byte stream of the
SQL query to run against the server
// This
query searches for contacts with the givenName the begins with 'wes'
// Link to Exchange store property names
byte[] contents = Encoding.UTF8.GetBytes(string.Format(
@"<?
xml version=""1.0""?>
<g:searchrequest xmlns:g=""DAV:"">
<g:
sql>
SELECT
""urn:schemas:contacts:sn"", ""urn:schemas:contacts:givenName"",
""urn:schemas:contacts:email1"", ""urn:schemas:contacts:telephoneNumber""
FROM
Scope('SHALLOW TRAVERSAL OF ""{0}/public/{1}""')
</g:
sql>
</g:searchrequest>",
server, Folders));
HttpWebRequest request = HttpWebRequest.Create(
uri) as HttpWebRequest;
request.Credentials = credentials;
request.Method = "SEARCH";
request.ContentLength = contents.Length;
request.ContentType = "text/
xml";
//request.CookieContainer = new CookieContainer();
// Keep in mind you may actually want to cache these cookies for other requests
//request.CookieContainer.Add(GetAuthCookies(server, credentials));
using (Stream requestStream = request.GetRequestStream())
requestStream.Write(contents, 0, contents.Length);
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
using (Stream responseStream = response.GetResponseStream())
{
// Process the response as an
XML document
XmlDocument document = new XmlDocument();
document.Load(responseStream);
foreach (XmlElement element in document.GetElementsByTagName("a:prop"))
{
// Do work with data returned for each contact
i++;
Console.WriteLine(i.ToString() + ":");
Console.WriteLine("Name: {0} {1}\nEmail: {2}\nPhone: {3}",
(element["d:givenName"] != null ? element["d:givenName"].InnerText : ""),
(element["d:sn"] != null ? element["d:sn"].InnerText : ""),
(element["d:email1"] != null ? element["d:email1"].InnerText : ""),
(element["d:telephoneNumber"] != null ? element["d:telephoneNumber"].InnerText : ""));
Console.WriteLine("--------------------------------------------------");
}
}
}