Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to delete objects by using WebDAV in Visual C# on a computer that is running Exchange 2000 Server


View products that this article applies to.

This article was previously published under Q313122

↑ Back to the top


Summary

This article describes how to delete objects by using Web Distributed Authoring and Versioning (WebDAV) in Microsoft Visual C# on a computer that is running Microsoft Exchange 2000 Server. This article also includes a code sample that uses the HttpWebRequest class and the HttpWebResponse class in the "System.Net" namespace to delete objects.

↑ Back to the top


More information

To delete objects by using WebDAV in Visual C# on a computer that is running Exchange 2000, follow these steps:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. In the Visual C# Projects types list, click Console Application.

    Note In Visual Studio 2005, click Console Application in the Visual C# list.

    In Visual Studio .NET, Class1.cs is created by default. In Visual Studio 2005, Program.cs is created by default.
  4. In the code window, replace the code with the following:
    using System;
    using System.Net;
    using System.IO;                   
    
    namespace WebDavNET
    {
        /// <summary>
        /// Summary description for Class1.
        /// </summary>
        class Class1
        {
            static void Main(string[] args)
            {
                try 
                {
                    // TODO: Replace with the URL of an object in Exchange 2000.
                    string sUri = "http://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();
    
                    // Clean up
                    myCred = null;
                    myCredentialCache = null;
                    HttpWRequest = null;
                    HttpWResponse = null;
                    strm = null;
                    sr = null;
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0} Exception caught.", e);
                }
            }
        }
    
    }
  5. Search for TODO in the code, and then modify the code for your environment.
  6. Press F5 to build and to run the program.
  7. Make sure that the object was deleted.

↑ Back to the top


Keywords: KB313122, kbhowto, kbmsg, kbcode

↑ Back to the top

Article Info
Article ID : 313122
Revision : 7
Created on : 12/11/2006
Published on : 12/11/2006
Exists online : False
Views : 356