Microsoft KB Archive/313121

= How to create an Outlook Calendar folder by using WebDAV in Visual C# =

Article ID: 313121

Article Last Modified on 10/25/2007

-

APPLIES TO


 * Microsoft Exchange Server 2003 Enterprise Edition
 * Microsoft Exchange Server 2003 Standard Edition
 * Microsoft Exchange 2000 Server Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# 2005

-



This article was previously published under Q313121



SUMMARY
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 &quot;MKCOL&quot;, 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.



Sample
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, click New, and then click Project. 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.  In the code window, replace the whole code with the following: using System; using System.Net; using System.IO;

namespace WebDavNET { ///   /// Summary description for Class1. ///  class Class1 {   static void Main(string[] args) {     try {       /*        '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 = &quot;http://ExchServer/Exchange/Administrator/Inbox/NewApptFolder&quot;; System.Uri myUri = new System.Uri(sUri); HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(myUri);

string sQuery; sQuery = &quot;&quot; + &quot;&quot; + &quot;&quot; + &quot;IPF.Appointment</ex:outlookfolderclass>&quot; + &quot;</a:prop></a:set>&quot; + &quot;</a:propertyupdate>&quot;;

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

// Set Headers HttpWRequest.KeepAlive = false; HttpWRequest.Headers.Set(&quot;Pragma&quot;, &quot;no-cache&quot;); HttpWRequest.ContentType = &quot;text/xml&quot;; HttpWRequest.ContentLength = sQuery.Length;

//Set the request timeout to 5 minutes HttpWRequest.Timeout = 300000; // Set the request method HttpWRequest.Method = &quot;MKCOL&quot;;

// 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 QueryStream.Write(ByteQuery,0,ByteQuery.Length); QueryStream.Close;

// 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(&quot;Status Code: {0}&quot;, sStatus); // Get the request headers string sReqHeaders = HttpWRequest.Headers.ToString; Console.WriteLine(sReqHeaders);

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

// Close Stream strm.Close;

// Clean Up       myCred = null; myCredentialCache = null; HttpWRequest = null; HttpWResponse = null; QueryStream = null; strm = null; sr = null; }     catch (Exception e)      { Console.WriteLine(&quot;{0} Exception caught.&quot;, e); }   }  }

} </li> Modify the code wherever you see &quot;TODO&quot;.</li> Press F5 to build and run the program.</li> Verify that the folder is created.</li></ol>

Keywords: kbmsg kbxml kbcode kbhowtomaster kbhowto KB313121

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.