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 Collaboration Data Objects for Exchange 2000 Library in Visual C#


View products that this article applies to.

This article was previously published under Q310197
Caution ADO and ADO MD have not been fully tested in a Microsoft .NET Framework environment. They may cause intermittent issues, especially in service-based applications or in multithreaded applications. The techniques that are discussed in this article should only be used as a temporary measure during migration to ADO.NET. You should only use these techniques after you have conducted complete testing to make sure that there are no compatibility issues. Any issues that are caused by using ADO or ADO MD in this manner are unsupported. For more information, see the following article in the Microsoft Knowledge Base:
840667 (http://support.microsoft.com/kb/840667/ ) You receive unexpected errors when using ADO and ADO MD in a .NET Framework application

↑ Back to the top


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.

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


Keywords: KB310197, kbhowtomaster, kbcode, kbxml, kbmsg

↑ Back to the top

Article Info
Article ID : 310197
Revision : 6
Created on : 11/29/2007
Published on : 11/29/2007
Exists online : False
Views : 502