How to create a contact by using ADO and ExOLEDB in Visual Basic .NET (314362)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .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 Q314362
For a Microsoft Visual C# .NET version of this article, see 310199.

SUMMARY

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

MORE INFORMATION

Note You must run the following code sample on a computer that is running Microsoft Exchange 2000 Server for the code sample to run correctly.

Sample

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the Visual Basic Projects list, click Console Application.

    By default, Module1.vb is created.
  4. Add a reference to the Microsoft CDO for Exchange 2000 Library. To do so, follow these steps:
    1. On the Project menu, click Add Reference.
    2. Click the COM tab, locate the Microsoft CDO for Exchange 2000 Library, and then 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 In Microsoft .NET Framework applications, Collaboration Data Objects for Exchange 2000 (CDOEX) is supported only through a COM interop. 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

  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 code:
    Module Module1
    
        Sub Main()
            ' TODO: Replace with your new item URL
            Dim sURL As String
            sURL = "http://<ExchServer>/Exchange/<UserAlias>/Contacts/Test.eml"
    
            Dim oCn As ADODB.Connection = New ADODB.Connection()
            Dim oRc As ADODB.Record = New ADODB.Record()
            Dim oFields As ADODB.Fields
    
            oCn.Provider = "exoledb.datasource"
    
            oCn.Open(sURL, "", "", 0)
            If oCn.State = 1 Then
                Console.WriteLine("Good Connection")
            Else
                Console.WriteLine("Bad Connection")
                Return
            End If
    
            oRc.Open(sURL, oCn, _
             ADODB.ConnectModeEnum.adModeReadWrite, _
             ADODB.RecordCreateOptionsEnum.adCreateNonCollection, _
             ADODB.RecordOpenOptionsEnum.adOpenRecordUnspecified, _
             "", "")
            If oRc.State = ADODB.ObjectStateEnum.adStateOpen Then
                Console.WriteLine("Record Open Success")
            Else
                Console.WriteLine("Record Open Fails")
                Return
            End If
    
    
            oFields = oRc.Fields
    
            oFields.Item("DAV:contentclass").Value = "urn:content-classes:person"
            oFields.Item("http://schemas.microsoft.com/exchange/outlookmessageclass").Value = "IPM.Contact"
            oFields.Item("urn:schemas:contacts:cn").Value = "David Jones"
            oFields.Item("urn:schemas:contacts:nickname").Value = "Dave"
            oFields.Item("urn:schemas:contacts:title").Value = "Engineer"
            oFields.Item("urn:schemas:contacts:department").Value = "Microsoft"
            oFields.Item("urn:schemas:contacts:email1").Value = "someone@example.com"
            oFields.Item("urn:schemas:contacts:fileas").Value = "Dave Jones"
    
            oFields.Update()
    
            oCn = Nothing
            oRc = Nothing
            oFields = Nothing
        End Sub
    
    End Module
  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 was created.

REFERENCES

For information about the urn:content-classes:person content class, visit the following Microsoft 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

287534 PRB: ExOLEDB provider cannot access or create items remotely


Modification Type:MajorLast Reviewed:3/25/2004
Keywords:kbhowto KB314362 kbAudDeveloper