Microsoft KB Archive/310200

= How to use the CDOEX Library to create a contact by using an Item object in Visual C# =

Article ID: 310200

Article Last Modified on 11/29/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft ActiveX Data Objects 2.5
 * Microsoft ActiveX Data Objects 2.6
 * Microsoft ActiveX Data Objects 2.7
 * Collaboration Data Objects for Exchange 2000
 * Microsoft Exchange 2000 Server Standard Edition
 * Microsoft Visual C# 2005

-



This article was previously published under Q310200



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 You receive unexpected errors when using ADO and ADO MD in a .NET Framework application



SUMMARY
This article describes how to use the Microsoft Collaboration Data Objects (CDO) for Exchange 2000 (CDOEX) Library to create a contact by using an Item object in Microsoft Visual C#.



MORE INFORMATION
To use the CDOEX Library to create a contact by using an Item object in Visual C#, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. In the Visual C# Projects types list, click Console Application.

By default, Class1.cs is created.

Note In Visual Studio 2005, click Console Application in the Visual C# list. By default, Program.cs is created. Add a reference to the CDOEX Library. To do so, follow these steps:  On the Project menu, click Add Reference. Click the COM tab, locate Microsoft CDO for Exchange 2000 Library, and then click Select.</li> In the Add References dialog box, click OK.</li> If you are prompted to generate wrappers for the libraries that you selected, click Yes.</li></ol>

Note CDOEX is supported only through a COM interop.</li> If Visual Studio .NET or Visual Studio 2005 does not add a reference to the Microsoft ActiveX Data Objects 2.5 Library, repeat step 4 to add a reference to it.</li>  In the code window, replace the code with the following: using System;

namespace Samples {   class Class1 {       static void Main(string[] args) {           try {           CDO.Item oItem = new CDO.Item;

// TODO: Replace with your folder URL string sURL = &quot;http://ExchServer/Exchange/UserAlias/Contacts&quot;;

ADODB.Fields oFields; oFields = oItem.Fields;

oFields[&quot;DAV:contentclass&quot;].Value = &quot;urn:content-classes:person&quot;; oFields[&quot;http://schemas.microsoft.com/exchange/outlookmessageclass&quot;].Value = &quot;IPM.Contact&quot;; oFields[&quot;urn:schemas:contacts:cn&quot;].Value = &quot;David Jones&quot;; oFields[&quot;urn:schemas:contacts:nickname&quot;].Value = &quot;Dave&quot;; oFields[&quot;urn:schemas:contacts:title&quot;].Value = &quot;Engineer&quot;; oFields[&quot;urn:schemas:contacts:department&quot;].Value = &quot;DSX Messaging&quot;; oFields[&quot;urn:schemas:contacts:email1&quot;].Value = &quot;someone@example.com&quot;;

oFields.Update;

oItem.DataSource.SaveToContainer(sURL, null, // has oCn or not, OK               ADODB.ConnectModeEnum.adModeReadWrite,                 ADODB.RecordCreateOptionsEnum.adCreateNonCollection,                 ADODB.RecordOpenOptionsEnum.adOpenSource,                  &quot;&quot;, &quot;&quot;);

oItem = null; oFields = null; }           catch (Exception e)            { Console.WriteLine(&quot;{0} Exception caught.&quot;, e); }                  }        } } </li> Search for TODO in the code, and then modify the code for your environment.</li> Press F5 to build and to run the program.</li> Make sure that the contact was created.</li></ol>

<div class="references_section">