Creating Outlook Calendar Folder using WebDAV & C#.Net

Code snippet to create Outlook Calendar Folder using WebDAV & C#.Net

// 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;

Note: This code snippet shows how to use WebDAV to create a Microsoft Outlook Calendar folder.

  • It initially sets the method of the HttpWebRequest class to "MKCOL"

  • Then sends the request with the user credentials to the Exchange server.

  • It uses the HttpWebResponse method to receive the response.

The HttpWebRequest and the HttpWebResponse methods are available in the System.Net namespace

Comments (1)
  1. Code snippet to create Outlook Calendar Folder using WebDAV &amp; C#.Net // TODO: Replace with the URL

Comments are closed.

Skip to main content