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 use the CDOEX Library to create a contact in Visual C#


View products that this article applies to.

This article was previously published under Q310195
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


Summary

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

↑ Back to the top


More information

To use the CDOEX Library to create a contact in Visual C# .NET or Visual C# 2005, follow these steps:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. In the Visual C# Projects list, click Console Application.

    By default, Class1.cs is created.

    Note In Microsoft Visual Studio 2005, click Console Application in the Visual C# list. By default, Program.cs is created.
  4. Add a reference to the CDOEX Library. To do so, follow these steps:
    1. On the Project menu, click Add Reference.
    2. Click the COM tab, locate 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.
    4. If you are prompted to generate wrappers for the libraries that you selected, click Yes.
    Note CDOEX is supported only through a COM interop.
  5. Repeat step 4 to add a reference to the Microsoft ActiveX Data Objects 2.5 Library.
  6. In the code window, replace the code with the following:
    using System;
    
    namespace Samples
    {
    	class Class1
    	{
    		static void Main(string[] args)
    		{
    			try 
    			{
    			CDO.Person oPerson = new CDO.Person();
    
                            // TODO: Replace with your Contacts folder URL
    			string sURL = "Http://<ExchServer>/Exchange/<UserAlias>/Contacts";
     			
    			ADODB.Connection oCn = new ADODB.Connection();
    			oCn.Provider = "exoledb.datasource";
    
    			oCn.Open(sURL, "", "", 0);  
    			if(oCn.State == 1)
    			{
    				Console.WriteLine("Connection Successful");
    			}
    			else
    			{
    				Console.WriteLine("Connection Failed");
    				return;
    			}			
    			
    			oPerson.Title = "Engineer";
    			oPerson.FirstName = "First";
    			oPerson.LastName = "Last";
    			oPerson.Company = "CompanyName";
    			oPerson.Email = "someone@example.com";
    			
    			oPerson.DataSource.SaveToContainer(sURL, null, 
    				ADODB.ConnectModeEnum.adModeReadWrite, 
    				ADODB.RecordCreateOptionsEnum.adCreateNonCollection, 
    				ADODB.RecordOpenOptionsEnum.adOpenSource, 
    				"", "");
    
    			oCn.Close();
    
    			oPerson = null;
    			oCn = null;
    			}
    			catch (Exception e)
    			{
    				Console.WriteLine("{0} Exception caught.", e);
    			}			
    		}
            }
    }
  7. Search for TODO in the code, and then modify the code for your environment.
  8. Press F5 to build and to run the program.
  9. Make sure that the contact is created in the specified folder.

↑ Back to the top


References

For more information, visit the following MSDN Web site: For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
813349� Support policy for Microsoft Exchange APIs with .NET Framework applications

↑ Back to the top


Keywords: KB310195, kbhowto, kbmsg, kbcode

↑ Back to the top

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