How to create an Outlook Calendar folder by using the HttpWebRequest class in Visual Basic 2005 or in Visual Basic .NET (314192)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Exchange 2000 Server

This article was previously published under Q314192

INTRODUCTION

This article describes how to create a Microsoft Outlook Calendar folder by using the HttpWebRequest class and the HttpWebResponse class of the System.Net namespace. You create the Outlook Calendar folder on a Microsoft Exchange 2000 Server by using Microsoft Visual Basic .NET.

MORE INFORMATION

To create the Outlook Calendar folder, follow these steps:
  1. Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET.
  2. On the File menu, click New, and then click Project.
  3. In the Visual Basic Projects list, select Console Application.

    By default, Module1.vb is created.

    Note In Visual Studio 2005, the Visual Basic Projects list is named Visual Basic.
  4. In the code window, replace all the code with the following sample code.
    Imports System.Net
    Imports System.IO
    
    Module Module1
        Sub Main()
            'strFolderType    Name
            '
            'MailItems        IPF.Note
            'ContactItems     IPF.Contact
            'AppointmentItems IPF.Appointment
            'NoteItems        IPF.StickyNote
            'TaskItems        IPF.Task
            'JournalItems     IPF.Journal<BR/>
    
            ' TODO: Replace with the URL to the new folder.
            Dim sUri As String = "http://ExchServer/Exchange/Administrator/Inbox/NewFolder"
    
            Dim myUri As System.Uri = New System.Uri(sUri)
            Dim httpWRequest As HttpWebRequest = WebRequest.Create(myUri)
    
            Dim sQuery As String
            sQuery = "<?xml version='1.0'?>" & _
             "<a:propertyupdate xmlns:a='DAV:' " & _
             "xmlns:ex='http://schemas.microsoft.com/exchange/'>" & _
             "<a:set><a:prop>" & _
             "<ex:outlookfolderclass>IPF.Appointment</ex:outlookfolderclass>" & _
             "</a:prop></a:set>" & _
             "</a:propertyupdate>"
    
            ' Set credentials.
            ' TODO: Replace with appropriate user credentials.
            Dim myCred As NetworkCredential = New NetworkCredential("Domain\User", "Password")
            Dim MyCredentialCache As CredentialCache = New CredentialCache()
            MyCredentialCache.Add(myUri, "Basic", myCred)
            httpWRequest.Credentials = MyCredentialCache
    
            'Uncomment the following statement, and then comment the previous four statements if you want to 
            'use Integrated Windows authentication.
            'httpWRequest.Credentials = CredentialCache.DefaultCredentials
    
            'When you use Basic-type authentication, the username and the password are sent as base64-encoded text that is 
            'easily decoded. Microsoft recommends that you use Basic-type authentication instead of SSL to help protect
            'the username and the password.
    
            ' 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"
    
            ' The data must be stored in a byte array.
            Dim ByteQuery() As Byte = System.Text.Encoding.ASCII.GetBytes(sQuery)
            httpWRequest.ContentLength = ByteQuery.Length
            Dim QueryStream As Stream = 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.
            Dim httpWResponse As HttpWebResponse = httpWRequest.GetResponse()
    
            ' Get Status and Headers.
            Dim iStatCode As Integer = httpWResponse.StatusCode
            Dim sStatus As String = iStatCode.ToString()
            Console.WriteLine("Status: {0} {1}", sStatus, httpWResponse.StatusDescription.ToString())
    
            Console.WriteLine("Request Headers:")
            Console.WriteLine(httpWRequest.Headers.ToString())
            Console.WriteLine("Response Headers:")
            Console.WriteLine(httpWResponse.Headers.ToString())
    
            ' Get Response Stream.
            Dim strm As Stream = httpWResponse.GetResponseStream()
    
            ' Read the Response Steam.
            Dim sr As StreamReader = New StreamReader(strm)
            Dim sText As String = sr.ReadToEnd()
            Console.WriteLine("Response: {0}", sText)
    
            ' Close the Stream.
            strm.Close()
    
            ' Clean up.
            httpWRequest = Nothing
            httpWResponse = Nothing
            MyCredentialCache = Nothing
            myCred = Nothing
            strm = Nothing
            sr = Nothing
        End Sub
    End Module
    
  5. Modify the sample code where you see TODO in the sample code.
  6. Press F5 to build and to run the program.
  7. Verify that the Outlook Calendar folder is created.

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbXML kbhowto KB314192 kbAudDeveloper