How to create an Outlook Calendar folder by using Collaboration Data Objects for Exchange 2000 Library in Visual C# (310197)



The information in this article applies to:

  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • Microsoft Exchange 2000 Server
  • Collaboration Data Objects for Exchange 2000
  • ActiveX Data Objects (ADO) 2.5
  • Microsoft Exchange Server 2003 Standard Edition
  • Microsoft Exchange Server 2003 Enterprise Edition

This article was previously published under Q310197

INTRODUCTION

This article describes how to use Microsoft Collaboration Data Objects (CDO) for Microsoft Exchange 2000 Library to create a Microsoft Outlook Calendar folder in Microsoft Visual C#.

Note To work correctly, this code must be run on an Exchange server.

back to the top

Create the Outlook Calendar folder

To create an Outlook Calendar folder by using Visual Studio .NET, follow these steps:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, click New, and then click Project.
  3. Under Project Types, click Visual C# Projects.

    Note In Visual Studio 2005, click Visual C# under Project Types.
  4. Under Templates, double-click Console Application.

    In Visual Studio .NET, Class1.cs is created by default. In Visual Studio 2005, Program.cs is created by default.
  5. Add a reference to the CDO for Exchange 2000 Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the COM tab, click Microsoft CDO for Exchange 2000 Library, and then click Select.

      Note In Visual Studio 2005, you do not have to click Select.
    3. In the Add References dialog box, click OK to accept your selection.

      If you are prompted to generate wrappers for the libraries that you selected, click Yes.
  6. Follow steps 5a through 5c to add a reference to the Microsoft ActiveX Data Objects 2.5 Library.
  7. In the code window, replace all the code with the following code:
    using System;
    
    namespace Samples
    {
    	class Class1
    	{
    		static void Main(string[] args)
    		{
    			try 
    			{
    		        /*
    		         sType            Value
    		
    		         MailItems        IPF.Note
    		         ContactItems     IPF.Contact
    		         AppointmentItems IPF.Appointment
    		         NoteItems        IPF.StickyNote
    		         TaskItems        IPF.Task
    		         JournalItems     IPF.Journal
    		        */                        
    
                            // Create a Calendar folder.
                            String sType = "IPF.Appointment";
    
    			ADODB.Connection oCn = new ADODB.Connection();
    			ADODB.Record oRc = new ADODB.Record();
    
    			ADODB.Fields oFields;
    			ADODB.Field oField;
    
    			string sFolderTypeProperty;
    			sFolderTypeProperty = "http://schemas.microsoft.com/exchange/outlookfolderclass";
    
                            // TODO: Replace the URL with your new folder URL.
    			string sFdUrl = "http://ExchServer/Exchange/UserAlias/Inbox/NewFolder";
    
    			oCn.Provider = "exoledb.datasource";
    
    			oCn.Open(sFdUrl, "", "", 0);  
    			if(oCn.State == 1)
    			{
    				Console.WriteLine("Good Connection");
    			}
    			else
    			{
    				Console.WriteLine("Bad Connection");
    			}
    
    			oRc.Open(sFdUrl, oCn,   
    				ADODB.ConnectModeEnum.adModeReadWrite, 
    				ADODB.RecordCreateOptionsEnum.adCreateCollection, 
    				ADODB.RecordOpenOptionsEnum.adOpenSource, 
    				"", "");  
    
    			// Get fields.
    			oFields = oRc.Fields;
    
    			// The property has been set to a variable.
    			oField = oFields[sFolderTypeProperty];
    			oField.Value = sType;
    			
    			oFields.Update();
    			
    			oRc.Close();
    			oCn.Close();
    
    			oCn = null;
    			oRc = null;
    			oFields = null;
    			oField = null;
    			}
    			catch (Exception e)
    			{
    				Console.WriteLine("{0} Exception caught.", e);
    			}			
    		}
            }
    }
    					
  8. Modify the code accordingly where you see the TODO comment.
  9. Press F5 to build and to run the program.
  10. Verify that the Outlook folder was created.
back to the top

Modification Type:MinorLast Reviewed:10/4/2006
Keywords:kbMsg kbXML kbcode kbHOWTOmaster KB310197 kbAudDeveloper