Adding/Removing a Manager Object with ADSI

Adding/Removing a Manager Object with 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.

Visual Basic

    
    ' Description: Adding/Removing a Manager Object with ADSI
    '
    ' Set the manager and direct report on a user object.
    '
    ' Visual Basic Project References
    '  - Microsoft ActiveX Data Objects 2.5 Library (msado15.dll)
    '  - Active DS Type Library (activeds.tlb)

    Private Sub Main()
    
    Dim objRootDSE As IADs
    Dim objUser As IADsUser
    Dim objUser2 As IADsUser
    Dim objConnection As New ADODB.Connection
    Dim objCommand As New ADODB.Command
    Dim RS As ADODB.Recordset
    Dim RS2 As ADODB.Recordset
    Dim varDomainNC As Variant
    Dim strQuery As String
    Dim strAlias As String
    Dim strReportToAdd As String
    Dim strManagerAlias As String
    Dim strAliasDN As String
    Dim varExchangeSD As Variant
    Dim varReports As Variant
    
    On Error Resume Next
    
    ' Set the alias for the user you want to modify.
    strAlias = "User1"
    
    ' Set the alias for the manager of the user you are
    ' modifying (User1).
    strManagerAlias = "User2"
    
    ' Set the alias for the direct report you want
    ' to add to the user you are modifying (User1).
    strReportToAdd = "User3"
    
    ' Get the configuration naming context.
    Set objRootDSE = GetObject("LDAP://RootDSE")
    varDomainNC = objRootDSE.Get("defaultNamingContext")
    
    ' Open the connection.
    objConnection.Provider = "ADsDSOObject"
    objConnection.Open "ADs Provider"
    
    ' Build the query to find the user (User1) based on the alias.
    strQuery = "<LDAP://" & varDomainNC & ">;(mailNickName=" & strAlias &     ");distinguishedname,ADsPath;subtree"
    
    objCommand.ActiveConnection = objConnection
    objCommand.CommandText = strQuery
    Set RS = objCommand.Execute
    
    ' If you get a result, get the object from the directory.
    If Not RS.EOF Then
    RS.MoveFirst
    Set objUser = GetObject(RS.Fields("ADsPath"))
    
    ' Print the current manager for this user (User1).
    Debug.Print "manager for ", strAlias, " is ", objUser.Get("manager")
    
    ' Change the manager for this user (User1).
    ' Build the query to find the user object of the manager (User2).
    strQuery = "<LDAP://" & varDomainNC & ">;(mailNickName=" & strManagerAlias & ");distinguishedname,ADsPath;subtree"
    objCommand.CommandText = strQuery
    Set RS2 = objCommand.Execute
    
    ' If you get a result, set the manager property of the user object 
    ' for User1.
    If Not RS2.EOF Then
    
    strAliasDN = RS2.Fields("distinguishedName")
    objUser.Put "manager", strAliasDN
    objUser.SetInfo
    End If
    
    ' Print the current manager for User1.
    Debug.Print "New manager for ", strAlias, " is ", objUser.Get("manager")
    
    
    '
    ' Add the new direct report.
    '
    
    'Print the current direct reports for the user object for User1.
    Call DisplayDirectReports(objUser, strAlias)
    
    ' Because the directReports property is a calculated property and is a back link,
    ' it cannot be modified directly. Modify the manager property of the user object   
    ' (strReportToAdd) for the user that you want to add as a direct report (User3).
    
    ' Build the query to find the direct report user object based on the alias (User3).
    Set RS2 = Nothing
    strQuery = "<LDAP://" & varDomainNC & ">;(mailNickName=" & strReportToAdd &    ");distinguishedname,manager,ADsPath;subtree"
    objCommand.CommandText = strQuery
    Set RS2 = objCommand.Execute
    
    ' If you get a result, set the manager property of the direct report
    ' (User3).
    If Not RS2.EOF Then
    RS2.MoveFirst
    Set objUser2 = GetObject(RS2.Fields("ADsPath"))
    
    ' Set the manager property for the direct report (User3) to
    ' the distinguished name of the user object (User1).
    strAliasDN = RS.Fields("distinguishedName")
    objUser2.Put "manager", strAliasDN
    objUser2.SetInfo
    End If
    
    Call DisplayDirectReports(objUser, strAlias)
    
    
    End If
    
    'Clean Up
    RS.Close
    objConnection.Close
    Set RS = Nothing
    Set RS2 = Nothing
    Set objCommand = Nothing
    Set objConnection = Nothing
    Set objUser = Nothing
    Set objUser2 = Nothing
    Set objRootDSE = Nothing

End Sub

Sub DisplayDirectReports(objUser As IADsUser, strAlias As String)
    
    Dim varReports As Variant
    Dim i As Integer
    
    ' Get the current direct reports for this user object.
    objUser.GetInfoEx Array("directReports"), 0
    varReports = objUser.GetEx("directReports")
    
    ' Print the current list of direct reports.
    Debug.Print "Direct reports for ", strAlias, " Currently are "
    For i = LBound(varReports) To UBound(varReports)
    Debug.Print varReports(i)
    Next i

End Sub

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.