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:
- Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
- On the File menu, click New, and then click Project.
- Under Project Types, click Visual C# Projects.
Note In Visual Studio 2005, click Visual C# under Project Types. - 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. - Add a reference to the CDO for Exchange 2000 Library. To do this, follow these steps:
- On the Project menu, click Add Reference.
- 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.
- 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.
- Follow steps 5a through 5c to add a reference to the Microsoft ActiveX Data Objects 2.5 Library.
- 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);
}
}
}
}
- Modify the code accordingly where you see the TODO comment.
- Press F5 to build and to run the program.
- Verify that the Outlook folder was created.