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 = "https://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>" +
  "</g:searchrequest>";

 NetworkCredential myCred;
 CredentialCache myCredentialCache;

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

 if (sUserName.Length != 0)
 {
  myCredentialCache.Add(myUri, "Basic", myCred);
  HttpWRequest.Credentials = myCredentialCache;
 }
 else
 {
  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);
 QueryStream.Close();

 //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
 strm.Close();

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

 return sText;