Creating a User Through CDO

Creating a User Through CDO

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The following example creates a user in a specified organization, assigns an offline address book (OAB), sets the Microsoft® Outlook® Web Access search criteria, and creates a mailbox. This example uses Collaboration Data Objects (CDO) to create the user and uses a function for Adding a User To a Group.

Visual Basic

'//////////////////////////////////////////////////////////////////////
' Function: createUserThroughCDO()
' Purpose:  Creates a user in the specified organization, assigns an offline address book,
'           sets their Outlook Web Access search criteria, and creates a mailbox.
'
' Input:    szServerName:           Name of Exchange server
'           szdomainName:           Domain of user
'           szExchangeOrg:          Name of Exchange organization
'           szAdminGroup:           Name of Exchange admin group
'           szStorageGroup:         Name of Exchange storage group
'           szstoreName:            Name of the store the mailbox is to be created in
'           szAlias:                Alias of user
'           szFirstName:            First name of user
'           szLastName:             Last name of user
'           szPassword:             User's password
'           szHostingOrgName:       Name of hosting container in the DS
'           szHostingOrgDomain:     SMTP suffix of user's org
'           szOrganizationalUnit:   Name of users org
'           szGroupName:            Name of group to add user to
'           szDirectoryServer:      Name of the Directory Server
'           szAdminUserName:        Administrator user name
'           szAdminPassword:        Administrator password
'
' Output:   createUserThroughCDO:   Contains Error code (if any)
'
' Note:  In order for this example to function correctly, it may be necessary to include
' references to the following libraries: Active DS Type Library, Microsoft CDO for
' Exchange Management Library, Microsoft Cluster Service Automation Classes,
' Microsoft CDO for Windows 2000 Library.
'//////////////////////////////////////////////////////////////////////
Public Function createUserThroughCDO(ByVal szServerName As String, _
                                     ByVal szDomainName As String, _
                                     ByVal szExchangeOrg As String, _
                                     ByVal szAdminGroup As String, _
                                     ByVal szStorageGroup As String, _
                                     ByVal szstoreName As String, _
                                     ByVal szAlias As String, _
                                     ByVal szFirstName As String, _
                                     ByVal szLastName As String, _
                                     ByVal szPassword As String, _
                                     ByVal szHostingOrgName As String, _
                                     ByVal szHostingOrgDomain As String, _
                                     ByVal szOrganizationalUnit As String, _
                                     ByVal szGroupName As String, _
                                     ByVal szDirectoryServer, _
                                     ByVal szAdminUserName, _
                                     ByVal szAdminPassword) As Integer

    Dim objPerson As CDO.Person
    Dim objMailbox As cdoexm.IMailboxStore
    Dim szConnString As String
    Dim szOABlocation As String
    Dim szLdapDomain As String
    Dim szaDomTokens() As String
    Dim szDomainDN As String

    On Error GoTo errhandler

    ' Puts the domain specified into an ldap domain string.
    szaDomTokens = Split(szDomainName, ".", -1, 1)
    szDomainDN = Join(szaDomTokens, ",dc=")
    szDomainDN = "dc=" & szDomainDN
    szLdapDomain = szDomainDN

    ' Build the necessary LDAP path to create the user.
    szConnString = "LDAP://" + szDirectoryServer + "/" + _
                   "cn=" + szAlias + ",OU=" + szOrganizationalUnit + _
                   ",OU=" + szHostingOrgName + "," + szLdapDomain

    ' Build the necessary LDAP path to this users's OAB's.

    szOABlocation = "cn=" + szOrganizationalUnit + _
                    ",cn=Offline Address Lists,cn=Address Lists Container,cn=" + _
                    szExchangeOrg + ",cn=Microsoft Exchange,cn=Services,cn=Configuration," + szLdapDomain

    ' First, create the user in Active Directory.

    Set objPerson = CreateObject("CDO.Person")

    With objPerson
        .FirstName = szFirstName
        .LastName = szLastName
        .Fields("sAMAccountName") = szAlias + "@" + szHostingOrgDomain
        .Fields("userPrincipalName") = szAlias + "@" + szHostingOrgDomain
        .Fields("userAccountControl") = 66048 ' set password doesn't expire
        .Fields("userPassword") = szPassword
        .Fields("msexchQueryBaseDN") = "OU=" + szOrganizationalUnit + "," + "OU=" + szHostingOrgName + "," + szLdapDomain
        .Fields("mailNickname") = szAlias
        .Fields("msExchUseOAB") = szOABlocation
        .Fields.Update
        .DataSource.SaveTo szConnString, , , , , szAdminUserName, szAdminPassword
    End With

    ' Create the mailbox in the specified location.

    Set objMailbox = objPerson.GetInterface("IMailboxStore")
    objMailbox.CreateMailbox "LDAP://" + szDirectoryServer + _
                             "/CN=" + szstoreName + ",CN=" + szStorageGroup + _
                             ",CN=InformationStore,CN=" + szServerName + _
                             ",CN=Servers,CN=" + szAdminGroup + _
                             ",CN=Administrative Groups,CN=" + szExchangeOrg + _
                             ",CN=Microsoft Exchange,CN=Services,CN=Configuration," + _
                             szLdapDomain
    With objPerson
        .Email = szAlias + "@" + szHostingOrgDomain
        .DataSource.Save
    End With

    ' See if a group name was passed in.  If so, add this user to that group.

    If szGroupName <> "" Then
        addUserToGroup szDomainName, _
                       szAlias, _
                       szGroupName, _
                       szHostingOrgName, _
                       szOrganizationalUnit, _
                       szHostingOrgDomain, _
                       True, _
                       szAdminUserName, _
                       szAdminPassword, _
                       szDirectoryServer
    End If

    createUserThroughCDO = 0

    ' Clean up.
    Set objPerson = Nothing
    Set objMailbox = Nothing
    Exit Function

    ' Error handling.
errhandler:

    createUserThroughCDO = 1
    ' Implement error logging here.
    Set objPerson = Nothing
    Set objMailbox = Nothing

End Function

Send us your feedback about the Microsoft Exchange Server 2003 SDK.

Build: June 2007 (2007.618.1)

© 2003-2006 Microsoft Corporation. All rights reserved. Terms of use.