Share via


Adding a User To a Group

Adding a User To a Group

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 adds a specified user or group to a specified group.

Visual Basic

'//////////////////////////////////////////////////////////////////////
' Function: addUserToGroup()
' Purpose:  Adds the specified user (or group) to the specified group.
'
' Input:    szDomainName:          Domain of the Exchange org
'           szUserAlias:           Alias of user (or group) to add
'           szGroupAlias:          Name of Exchange organization
'           szHostingOrgName:      Name of hosting container
'           szOrganizationalUnit:  Name of hosting organization
'           szSMTPAddress:         SMTP address of user & group
'           boolUser:              User or group?  T/F
'           szUserName:            Admin username
'           szUserPwd:             Admin pwd
'           szDirectoryServer:     Name of the Directory Server
'
' Output:   addUserToGroup:   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 addUserToGroup(ByVal szDomainName As String, _
                               ByVal szUserAlias As String, _
                               ByVal szGroupAlias As String, _
                               ByVal szHostingOrgName As String, _
                               ByVal szOrganizationalUnit As String, _
                               ByVal szSMTPAddress As String, _
                               ByVal boolUser As Boolean, _
                               ByVal szUserName As String, _
                               ByVal szUserPwd As String, _
                               ByVal szDirectoryServer) As Integer

    Dim szLdapDomain As String
    Dim szConnStringGroup As String
    Dim szConnStringUser As String
    Dim objGrp As IADs
    Dim objUsr As IADs
    Dim objLdap As IADsOpenDSObject
    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

    ' Create the ldap path for the group.
    szConnStringGroup = "LDAP://" + szDirectoryServer + _
                        "/cn=" + szGroupAlias + "@" + szSMTPAddress + _
                        ",OU=" + szOrganizationalUnit + ",OU=" + _
                        szHostingOrgName + "," + szLdapDomain

    ' Create the ldap path for the object to ADD to the group.
    If boolUser Then
        szConnStringUser = "LDAP://" + szDirectoryServer + _
                           "/cn=" + szUserAlias + ",OU=" + szOrganizationalUnit + _
                           ",OU=" + szHostingOrgName + "," + szLdapDomain
    Else
        szConnStringUser = "LDAP://" + szDirectoryServer + _
                           "/cn=" + szUserAlias + "@" + szSMTPAddress + _
                           ",OU=" + szOrganizationalUnit + ",OU=" + _
                           szHostingOrgName + "," + szLdapDomain
    End If

    ' Get handles to the objects.

    Set objLdap = GetObject("LDAP:")

    Set objGrp = objLdap.OpenDSObject(szConnStringGroup, _
                                      szUserName, _
                                      szUserPwd, _
                                      ADS_SECURE_AUTHENTICATION)

    Set objUsr = objLdap.OpenDSObject(szConnStringUser, _
                                      szUserName, _
                                      szUserPwd, _
                                      ADS_SECURE_AUTHENTICATION)

    ' Join the user/group to the group.

    With objGrp
        .Add objUsr.ADsPath
        .SetInfo
    End With

    addUserToGroup = 0

    ' Clean up.
    Set objGrp = Nothing
    Set objUsr = Nothing
    Set objLdap = Nothing
    Exit Function

    ' Error handling.
errhandler:

    addUserToGroup = 1
    ' Implement error handling here.
    Set objGrp = Nothing
    Set objUsr = Nothing
    Set objLdap = 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.