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 the HttpWebRequest class in Visual Basic 2005 or in Visual Basic .NET


View products that this article applies to.

This article was previously published under Q314192

↑ Back to the top


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.

↑ Back to the top


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.

↑ Back to the top


Keywords: KB314192, kbhowto, kbxml, kbvs2005applies, kbvs2005swept

↑ Back to the top

Article Info
Article ID : 314192
Revision : 5
Created on : 12/6/2006
Published on : 12/6/2006
Exists online : False
Views : 446