Directory Services

Creating Contact Objects in ADAM

[This documentation is preliminary and subject to change.]

To create a Contact object, bind to the object that will contain the object, create an Contact object, set its properties, and save the object to the directory store.

The following code example assumes that the schema of the ADAM instance has been extended by Adding an ADAM Contact Class.

The following VBScript code example defines a function that uses the Create method to create Contact objects in a selected organization object.

' Create Contacts.

Option Explicit

Dim lngErrNumber  ' Error number.
Dim objADAM	 ' Binding object.
Dim strPath	 ' Binding path.

' Construct ADAMsPath binding string.
' Change "localhost" to appropriate server.
' Change "389" to port for appropriate instance.
' Change "O=Fabrikam,C=US" to appropriate object.
strPath = "LDAP://localhost:389/O=Fabrikam,C=US"

Function lngCreateContact(objADAMPath, _
						strCN, _
						strSN, _
						strTitle, _
						strTelephoneNumber, _
						strGivenName, _
						strMail, _
						strManager)

	Dim objNewContact  ' New Contact to create.

	On Error Resume Next

	' Create new contact.
	Set objNewContact = objADAMPath.Create("Contact", "CN=" & strCN)

	' Set attributes.
	If strSN <> "" And strSN <> vbNullString Then
		objNewContact.Put "sn", strSN
	End If
	If strTitle <> "" And strTitle <> vbNullString Then
		objNewContact.Put "title", strTitle
	End If
	If strTelephoneNumber <> "" And strTelephoneNumber <> vbNullString Then
		objNewContact.Put "telephoneNumber", strTelephoneNumber
	End If
	If strGivenName <> "" And strGivenName <> vbNullString Then
		objNewContact.Put "givenName", strGivenName
	End If
	If strMail <> "" And strMail <> vbNullString Then
		objNewContact.Put "mail", strMail
	End If
	If strManager <> "" And strManager <> vbNullString Then
		objNewContact.Put "manager", strManager
	End If
	objNewContact.SetInfo

	' Return success or error.
	lngCreateContact = Err.Number

	Set objNewContact = Nothing

	On Error Goto 0

End Function

WScript.Echo "Bind to: " & strPath

On Error Resume Next

' Bind to the object.
Set objADAM = GetObject(strPath)

' Output error if the bind operation fails.
If Err.Number <> vbEmpty Then
	WScript.Echo "Error:   Bind failed."
	WScript.Quit
Else
	WScript.Echo "Success: Bind succeeded."
End If

On Error Goto 0

' Create first Contact.
' Manager not specified.
lngErrNumber = lngCreateContact(objADAM, _
								"Jane Clayton", _
								"Clayton", _
								"Office Manager", _
								"+1(206)555-0101", _
								"Jane", _
								"janeclayton@fabrikam.us", _
								"")
' Output success or error.
If lngErrNumber <> vbEmpty Then
	WScript.Echo "Error:   Create failed with error# " _
						 & Hex(lngErrNumber) & "."
Else
	WScript.Echo "Success: User created."
End If

' Create second Contact.
' Change "CN=Jane Clayton,O=Fabrikam,C=US" to appropriate manager.
lngErrNumber = lngCreateContact(objADAM, _
								"Jeff Smith", _
								"Smith", _
								"Office Assistant", _
								"+1(206)555-0111", _
								"Jeff", _
								"jeffsmith@fabrikam.us", _
								"CN=Jane Clayton,O=Fabrikam,C=US")
' Output success or error.
If lngErrNumber <> vbEmpty Then
	WScript.Echo "Error:   Create failed with error# " _
						 & Hex(lngErrNumber) & "."
Else
	WScript.Echo "Success: User created."
End If