Howto: WebDAV SEARCH using C#

' This example shows how to do a SEARCH using C#

// C# Example to SEARCH a mailbox.
// TODO:
//   Add a button and double click on it
//   Add a multi-line text box and make it big.
//   Add a project reference to System.Xml, an System.Net
//   Paste-in the code below
//   Do the TODO sections in the code.

using System.Xml;
using System.Net;
using System.IO;

private void button1_Click(object sender, System.EventArgs e)
 string sUri = "http://myemailserver/exchange/auser/inbox/test";
 string sUserName = ""; // TODO: Set only for Basic Authentication 
 string sPassword   = ""; // TODO: Set only if Basic 
 string sResponse   = "";

 sResponse = GetMessageListXML(sUri, sUserName, sPassword);
 textBox1.Text = sResponse;

private string GetMessageListXML(string sUri, string sUserName, string sPassword)

 System.Uri myUri  = new System.Uri(sUri);
 HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(myUri);

 string sQuery;
 sQuery = "<?xml version='1.0'?>" +
  "<g:searchrequest xmlns:g='DAV:'>" +
  "<g:sql>SELECT \"DAV:displayname\" " +
  "FROM SCOPE('SHALLOW TRAVERSAL OF \"" + sUri + "\"') " +
  "WHERE \"DAV:isfolder\" = false" +
  "</g:sql>" +

 NetworkCredential myCred;
 CredentialCache myCredentialCache;

 myCred = new NetworkCredential(sUserName, sPassword);
 myCredentialCache = new CredentialCache();

 if (sUserName.Length != 0)
  myCredentialCache.Add(myUri, "Basic", myCred);
  HttpWRequest.Credentials = myCredentialCache;
  HttpWRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
  // Next line is for double-hop.  This is a winform, so we don’t need it.
  //HttpWRequest.UnsafeAuthenticatedConnectionSharing = true;
 // Set some headers
 HttpWRequest.KeepAlive = false;
 HttpWRequest.Headers.Set("Pragma", "no-cache");

 HttpWRequest.Headers.Set("Translate", "f");
 HttpWRequest.Headers.Set("Depth", "0");
 HttpWRequest.ContentType = "text/xml";
 HttpWRequest.ContentLength = sQuery.Length;

 // set the request timeout to 5 min.
 HttpWRequest.Timeout = 300000;
 //set the request method
 HttpWRequest.Method = "SEARCH";

 // we need to store the data into a byte array
 Byte[] ByteQuery  = System.Text.Encoding.ASCII.GetBytes(sQuery);
 //HttpWRequest.ContentLength = ByteQuery.Length;
 HttpWRequest.ContentLength = ByteQuery.Length;
 Stream QueryStream = HttpWRequest.GetRequestStream();
 // write the data to be posted to the Request Stream
 QueryStream.Write(ByteQuery, 0, ByteQuery.Length);

 //Send Request and Get Response
 HttpWebResponse HttpWResponse  = (HttpWebResponse)HttpWRequest.GetResponse();

 // Get Response Stream
 Stream strm  = HttpWResponse.GetResponseStream();

 //Read the Response Steam;
 StreamReader sr  = new StreamReader(strm);
 String sText   = sr.ReadToEnd();

 //Close Stream

 // Clean Up
 HttpWRequest = null;
 HttpWResponse = null;
 myCredentialCache = null;
 myCred = null;
 QueryStream = null;
 strm = null;
 sr = null;

 return sText;

Comments (2)
  1. nick_cam says:

    Hi Daniel,

    I have a double-hop problem accessing exchange folders through an ASP.Net app. I’m at a client site and setting up Kerberos delegation will be a LOT of trouble.

    In your post you use UnsafeAuthenticatedConnectionSharing = true to address double-hop. I’ve tried this (also using ConnectionGroupNames) with no luck. Is there any specific setup/configuration required besides configuring app and IIS correctly – which I’m pretty positive I have already – to get this to fix the double-hop issue?


  2. nick_cam says:

    Hi Daniel,

    I got this working without using Kerberos delegation. In case anyone is having the same problem – I got it going by using PageMethods and javascript.

    I get some DB values from within a PageMethod and then pass the WebDav query to the OnSuccess javascript function of the PageMethod and construct and send a xmlHTTPRequest to Exchange in javascript – this bypasses the web server and the double hop problem.


Comments are closed.

Skip to main content