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 create an Outlook Calendar folder by using WebDAV in Visual C#

View products that this article applies to.


This article contains sample code that demonstrates how to use WebDAV to create a Microsoft Outlook Calendar folder. The sample sets the method of the HttpWebRequest class to "MKCOL", and then sends the request with the user credentials to the Microsoft Exchange 2000 server. The code uses the HttpWebResponse method to receive the response. The HttpWebRequest method and the HttpWebResponse method are in the System.Net namespace.

↑ Back to the top

More information


  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, click New, and then click Project.
  3. Select Console Application from the Visual C# Projects types. By default, Class1.cs is created in Visual Studio .NET. Program.cs is created in Visual Studio 2005.
  4. In the code window, replace the whole 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)
            'strFolderType    Name
            'MailItems        IPF.Note
            'ContactItems     IPF.Contact
            'AppointmentItems IPF.Appointment
            'NoteItems        IPF.StickyNote
            'TaskItems        IPF.Task
            'JournalItems     IPF.Journal
            // TODO: Replace with the URL of an object on Exchange Server
            string sUri = "http://ExchServer/Exchange/Administrator/Inbox/NewApptFolder";
            System.Uri myUri = new System.Uri(sUri);
            HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(myUri);
            string sQuery;
            sQuery = "<?xml version='1.0'?>" + 
              "<a:propertyupdate xmlns:a='DAV:' " + 
              "xmlns:ex=''>" + 
              "<a:set><a:prop>" + 
              "<ex:outlookfolderclass>IPF.Appointment</ex:outlookfolderclass>" + 
              "</a:prop></a:set>" + 
            // Set Credentials
            // TODO: Replace with appropriate user credentials
            NetworkCredential myCred = new NetworkCredential(@"DomainName\UserName", "UserPassword");
            CredentialCache myCredentialCache = new CredentialCache();
            myCredentialCache.Add(myUri, "Basic", myCred);
            HttpWRequest.Credentials = myCredentialCache;
            // Set Headers
            HttpWRequest.KeepAlive = false; 
            HttpWRequest.Headers.Set("Pragma", "no-cache");
            HttpWRequest.ContentType =  "text/xml";
            HttpWRequest.ContentLength = sQuery.Length;
            //Set the request timeout to 5 minutes
            HttpWRequest.Timeout = 300000;
            // Set the request method
            HttpWRequest.Method = "MKCOL";
            // Store the data in a byte array
            byte[] ByteQuery = System.Text.Encoding.ASCII.GetBytes(sQuery);
            HttpWRequest.ContentLength = ByteQuery.Length;
            Stream QueryStream = HttpWRequest.GetRequestStream();
            // Write the data to be posted to the Request Stream
            // Send Request and Get 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();
            // Read the Response Steam
            Stream strm = HttpWResponse.GetResponseStream();
            StreamReader sr = new StreamReader(strm);
            string sText = sr.ReadToEnd();
            Console.WriteLine("Response: {0}", sText);
            // Close Stream
            // Clean Up
            myCred = null;
            myCredentialCache = null;
            HttpWRequest = null;
            HttpWResponse = null;
            QueryStream = null;
            strm = null;
            sr = null;
          catch (Exception e)
            Console.WriteLine("{0} Exception caught.", e);
  5. Modify the code wherever you see "TODO".
  6. Press F5 to build and run the program.
  7. Verify that the folder is created.

↑ Back to the top

Keywords: KB313121, kbhowto, kbhowtomaster, kbcode, kbxml, kbmsg

↑ Back to the top

Article Info
Article ID : 313121
Revision : 8
Created on : 10/25/2007
Published on : 10/25/2007
Exists online : False
Views : 368