Deleting objects using WebDAV and C#.Net in Exchange Server

Code snippet to delete objects using WebDAV and C#.Net in Exchange Server Environment

 

               // TODO: Replace with the URL of an object in Exchange 2000.
                string sUri = "https://ExchServer/Exchange/Administrator/Inbox/NewApptFolder";

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

// Set the credentials.
// TODO: Replace with the appropriate user credential.
NetworkCredential myCred = new NetworkCredential(@"DomainName\UserName", "UserPassword");
CredentialCache myCredentialCache = new CredentialCache();
myCredentialCache.Add(myUri, "Basic", myCred);
HttpWRequest.Credentials = myCredentialCache;

// Set the headers.
HttpWRequest.KeepAlive = false;
HttpWRequest.Headers.Set("Pragma", "no-cache");

//Set the request timeout to 5 minutes.
HttpWRequest.Timeout = 300000;
// set the request method
HttpWRequest.Method = "DELETE";

// Send the request and get the response.
HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();

// Get the Status code.
int iStatCode = (int)HttpWResponse.StatusCode;
string sStatus = iStatCode.ToString();
Console.WriteLine("Status Code: {0}", sStatus);
// Get the request headers
string sReqHeaders = HttpWRequest.Headers.ToString();
Console.WriteLine(sReqHeaders);

// Read the response stream.
Stream strm = HttpWResponse.GetResponseStream();
StreamReader sr = new StreamReader(strm);
string sText = sr.ReadToEnd();
Console.WriteLine("Response: {0}", sText);

// Close the stream.
strm.Close();

 

Note: This code snippet uses the HttpWebRequest class and the HttpWebResponse class in the "System.Net" namespace to delete objects.