How to create a contact with ADO and the ExOLEDB provider by using Visual C# (310199)



The information in this article applies to:

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

This article was previously published under Q310199

SUMMARY

This article describes how to create a contact with ActiveX Data Objects (ADO) and the Microsoft Exchange OLE DB (ExOLEDB) provider by using Microsoft Visual C# .

MORE INFORMATION

To create a contact with ADO and the EXOLEDB provider by using Visual C# , 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 types list, click Console Application.

    By default, Class1.cs is created.


    Note In Microsoft Visual C# 2005, click Console Application under Visual C#. By default, Program.cs is created.
  4. Add a reference to the Microsoft ActiveX Data Objects 2.5 Library. To do so, follow these steps:
    1. On the Project menu, click Add Reference.
    2. Click the COM tab, locate Microsoft ActiveX Data Objects 2.5 Library, and then click Select.

      Note In Microsot Visual C# 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 Microsoft Collaboration Data Objects for Exchange 2000 (CDOEX) is supported only through a COM interop.
  5. In the code window, replace the code with the following:
    using System;
    
    namespace Samples
    {
    	class Class1
    	{
    		static void Main(string[] args)
    		{
    			try 
    			{
                            // TODO: Replace with your item URL
    			string sURL = "http:/<ExchServer>/Exchange/<UserAlias>/Contacts/Test.eml";
     			
    			ADODB.Connection oCn = new ADODB.Connection();
    			ADODB.Record oRc = new ADODB.Record();
    			ADODB.Fields oFields;
    
    			oCn.Provider = "exoledb.datasource";
    
    			oCn.Open(sURL, "", "", 0);  
    			if(oCn.State == 1)
    			{
    				Console.WriteLine("Connection Successful");
    			}
    			else
    			{
    				Console.WriteLine("Connection Unsuccessful");
    				return;
    			}			
    			
    			oRc.Open(sURL, oCn, 
    				ADODB.ConnectModeEnum.adModeReadWrite, 
    				ADODB.RecordCreateOptionsEnum.adCreateNonCollection,
    				ADODB.RecordOpenOptionsEnum.adOpenRecordUnspecified,
    				"", "");
    			
    			oFields = oRc.Fields;
    
    			oFields["DAV:contentclass"].Value = "urn:content-classes:person";
    			oFields["http://schemas.microsoft.com/exchange/outlookmessageclass"].Value = "IPM.Contact";
    			oFields["urn:schemas:contacts:cn"].Value = "David Jones";
    			oFields["urn:schemas:contacts:nickname"].Value = "Dave";
    			oFields["urn:schemas:contacts:title"].Value = "Engineer";
    			oFields["urn:schemas:contacts:department"].Value = "Department Name";
    			oFields["urn:schemas:contacts:email1"].Value = "someone@example.com";
    
    			
    			oFields.Update();
    
    
    			oCn = null;
    			oRc = null;
    			oFields = null;
    			}
    			catch (Exception e)
    			{
    				Console.WriteLine("{0} Exception caught.", e);
    			}			
    		}
            }
    }
  6. Search for TODO in the code, and then modify the code for your environment.
  7. Press F5 to build and to run the program.
  8. Make sure that the contact is created in the specified folder.

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


Modification Type:MinorLast Reviewed:10/4/2006
Keywords:kbMsg kbcode kbhowto KB310199 kbAudDeveloper