Creating a User Through ADSI
Creating a User Through ADSI
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 Active Directory® Service Interfaces (ADSI) to create the user and uses a function for Adding a User To a Group.
Visual Basic
'////////////////////////////////////////////////////////////////////// ' Function: createUserThroughADSI() ' Purpose: Creates a user in the specified organization, assigns an OAB, ' sets their Outlook Web Access search criteria, and creates a mailbox. This function ' uses ADSI to create the user. ' ' 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 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: createUserThroughADSI: 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 createUserThroughADSI(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 IADsUser Dim objContainer As IADsContainer Dim objMailbox As cdoexm.IMailboxStore Dim szConnString As String Dim szOABlocation As String Dim szLdapDomain As String Dim szSAMAccountName 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 LDAP path necessary to create the user. szConnString = "LDAP://" + szDirectoryServer + "/" + _ "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 Set objContainer = GetObject(szConnString) Set objPerson = objContainer.Create("User", "cn=" + szAlias) szSAMAccountName = szAlias + "@" + szHostingOrgDomain If Len(szSAMAccountName) > 20 Then szSAMAccountName = Mid(szSAMAccountName, 1, 20) End If With objPerson .FirstName = szFirstName .LastName = szLastName .Put "sAMAccountName", szSAMAccountName .Put "userPrincipalName", szAlias + "@" + szHostingOrgDomain .Put "userAccountControl", 66048 ' set password doesn't expire .Put "msexchQueryBaseDN", "OU=" + szOrganizationalUnit + "," + "OU=" + szHostingOrgName + "," + szLdapDomain .Put "mailNickname", szAlias .Put "msExchUseOAB", szOABlocation .SetInfo .SetPassword szPassword .AccountDisabled = False .SetInfo Set objMailbox = objPerson 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 .EmailAddress = szAlias + "@" + szHostingOrgDomain .SetInfo 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 createUserThroughADSI = 0 ' Clean up. Set objPerson = Nothing Set objMailbox = Nothing Set objContainer = Nothing Exit Function ' Error handling. errhandler: createUserThroughADSI = 1 Set objPerson = Nothing Set objMailbox = Nothing Set objContainer = Nothing 'Implement error logging here. 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.