Code permettant d'afficher une arborescence ad dans une treeview.

Contenu du snippet

Bonjour !
Je laisse juste ce petit bout de code qui peut vous aider si vous avez besoin de créer une treeview qui prends en charge AD.
Il peut servir pour afficher les utilisateurs avec les conteneurs AD, ou bien pour afficher une vue 'plane' type USRMGR dans NT4.
Les 'Keys' des nodes sont uniques grâce aux chemins LDAP...
C'est paramétrable ( indications dans le header en remarques ). Il vous faudra juste dessiner une treeview, puis ajouter une Imaglist, puis modifier un peu le code ( qui devrait être assez lisible pour cela ).

Bon courage

Yann A.

Source / Exemple :


'=====================================================================================
' DEFINITONS
'=====================================================================================
    Dim P1 As String
    Dim P2 As Boolean
    Dim P3 As Boolean
    Dim P4 As Boolean
    Dim P5 As Boolean
    Dim P6 As Boolean
    Dim P7 As Boolean
    Dim P8 As Boolean
    Dim P9 As Boolean

    Dim OBJ_AD_REFERENCE As Object
    Dim OBJ_TEMP_PARENT As Object
    Dim OBJ_TEMP As Object
    Dim OBJ_NODE As Node
    Dim OBJ_OBJECT_ICONE As Object
    Dim OBJ_IS_CONTAINER As Object
    
Public Sub DO_AD_TREEBUILD(OBJ_TREEVIEW As Object, OBJ_IMAGELIST As Object, STR_AD_REFERENCE As String, BOL_VIEW_DEFAULTCONTAINERS As Boolean, BOL_VIEW_OUS As Boolean, BOL_VIEW_USERS As Boolean, BOL_VIEW_GROUPS As Boolean, BOL_VIEW_COMPUTERS As Boolean, BOL_VIEW_PRINTERS As Boolean, BOL_VIEW_SHARES As Boolean, BOL_VIEW_TYPE As Boolean)
'======================================================================================
' OBJ_TREEVIEW      = Objet TreeView dans lequel construire la vue
' OBJ_IMAGELIST     = Objet Imagelist de référence pour les images
' P1 > STR_AD_REFERENCE              = chemin LDAP ( Conteneur ) qui sert de point de départ
' P2 > BOL_VIEW_DEFAULTCONTAINERS    = Visibilité des conteneurs par défaut AD
' P3 > BOL_VIEW_OUS                  = Visibilité des UOs
' P4 > BOL_VIEW_USERS                = Visibilité des Utilisateurs
' P5 > BOL_VIEW_GROUPS               = Visibilité des Groupes
' P6 > BOL_VIEW_COMPUTERS            = Visibilité des Ordinateurs
' P7 > BOL_VIEW_PRINTERS             = Visibilité des Imprimantes
' P8 > BOL_VIEW_SHARES               = Visibilité des Partages
' P9 > BOL_VIEW_TYPE                 = Type de vue utilisée ( True=Arbre, False=Plane )
'======================================================================================

    P1 = STR_AD_REFERENCE
    P2 = True
    P3 = True
    P4 = BOL_VIEW_USERS
    P5 = BOL_VIEW_GROUPS
    P6 = BOL_VIEW_COMPUTERS
    P7 = BOL_VIEW_PRINTERS
    P8 = BOL_VIEW_SHARES
    P9 = BOL_VIEW_TYPE
        
    OBJ_TREEVIEW.Nodes.Clear
    OBJ_TREEVIEW.ImageList = OBJ_IMAGELIST
    Set OBJ_AD_REFERENCE = GetObject(STR_AD_REFERENCE)
    
    If P9 = True Then
        Set OBJ_NODE = OBJ_TREEVIEW.Nodes.Add(, , OBJ_AD_REFERENCE.ADsPath, Mid(OBJ_AD_REFERENCE.Name, 4), 1)
        OBJ_NODE.Expanded = True
    End If
    
    Call DO_RECURSE_TREE(OBJ_TREEVIEW, OBJ_AD_REFERENCE)
    
End Sub

Private Sub DO_RECURSE_TREE(OBJ_TREEVIEW As Object, ByVal OBJ_TEMP_PARENT As Object)
'=====================================================================================
' BOUCLE RECURSIVE PARENT/ENFANT OBJETS ADS
'=====================================================================================
    For Each OBJ_TEMP In OBJ_TEMP_PARENT

    Call DO_ADD_NODE(OBJ_TREEVIEW, OBJ_TEMP_PARENT, OBJ_TEMP)
    If FUN_IS_CONTAINER(OBJ_TEMP) = True Then Call DO_RECURSE_TREE(OBJ_TREEVIEW, OBJ_TEMP)
    
    Next

End Sub

Private Sub DO_ADD_NODE(OBJ_TREEVIEW As Object, OBJ_TEMP_PARENT As Object, OBJ_TEMP As Object)
'=====================================================================================
' AJOUTE UN NOEUD ENFANT
'=====================================================================================
    If FUN_IS_VISIBLE(OBJ_TEMP) = False Then Exit Sub
    
    If P9 = True Then Set OBJ_NODE = OBJ_TREEVIEW.Nodes.Add(OBJ_TEMP_PARENT.ADsPath, tvwChild, OBJ_TEMP.ADsPath, Mid(OBJ_TEMP.Name, 4), FUN_OBJECT_ICON(OBJ_TEMP))
    If P9 = False And FUN_IS_CONTAINER(OBJ_TEMP) = False And FUN_OBJECT_ICON(OBJ_TEMP) <> 9 Then Set OBJ_NODE = OBJ_TREEVIEW.Nodes.Add(, , OBJ_TEMP.ADsPath, Mid(OBJ_TEMP.Name, 4), FUN_OBJECT_ICON(OBJ_TEMP))

End Sub

Private Function FUN_OBJECT_ICON(OBJ_OBJECT_ICON As Object)
'=====================================================================================
' FONCTION POUR RETROUNER L'ICONE D'UN OBJET
'=====================================================================================
    FUN_OBJECT_ICON = 9
    If FUN_IS_CONTAINER(OBJ_OBJECT_ICON) = True And OBJ_OBJECT_ICON.Class <> "organizationalUnit" Then FUN_OBJECT_ICON = 2
    If OBJ_OBJECT_ICON.Class = "organizationalUnit" Then FUN_OBJECT_ICON = 3
    If OBJ_OBJECT_ICON.Class = "user" Then FUN_OBJECT_ICON = 4
    If OBJ_OBJECT_ICON.Class = "group" Then FUN_OBJECT_ICON = 5
    If OBJ_OBJECT_ICON.Class = "computer" Then FUN_OBJECT_ICON = 6
    If OBJ_OBJECT_ICON.Class = "printer" Then FUN_OBJECT_ICON = 7
    If OBJ_OBJECT_ICON.Class = "volume" Then FUN_OBJECT_ICON = 8
    
End Function

Private Function FUN_IS_CONTAINER(OBJ_IS_CONTAINER As Object)
'=====================================================================================
' FONCTION DE DEFINITION D'UN CONTENEUR
'=====================================================================================
    FUN_IS_CONTAINER = False
    
    If OBJ_IS_CONTAINER.Class = "builtinDomain" Then FUN_IS_CONTAINER = True
    If OBJ_IS_CONTAINER.Class = "container" Then FUN_IS_CONTAINER = True
    If OBJ_IS_CONTAINER.Class = "lostAndFound" Then FUN_IS_CONTAINER = True
    If OBJ_IS_CONTAINER.Class = "organizationalUnit" Then FUN_IS_CONTAINER = True
    If OBJ_IS_CONTAINER.Class = "domainPolicy" Then FUN_IS_CONTAINER = True
    If OBJ_IS_CONTAINER.Class = "dfsConfiguration" Then FUN_IS_CONTAINER = True
    If OBJ_IS_CONTAINER.Class = "nTFRSSettings" Then FUN_IS_CONTAINER = True
    If OBJ_IS_CONTAINER.Class = "nTFRSReplicaSet" Then FUN_IS_CONTAINER = True
    If OBJ_IS_CONTAINER.Class = "fileLinkTracking" Then FUN_IS_CONTAINER = True
    If OBJ_IS_CONTAINER.Class = "rpcContainer" Then FUN_IS_CONTAINER = True
    If OBJ_IS_CONTAINER.Class = "linkTrackVolumeTable" Then FUN_IS_CONTAINER = True
    If OBJ_IS_CONTAINER.Class = "groupPolicyContainer" Then FUN_IS_CONTAINER = True
    
End Function

Private Function FUN_IS_VISIBLE(OBJ_IS_VISIBLE As Object)
'=====================================================================================
' FONCTION DE DEFINITION DE LA VISIBILITE D'UN OBJET
'=====================================================================================
    FUN_IS_VISIBLE = True
    
    If OBJ_IS_VISIBLE.Class = "user" And P4 = False Then FUN_IS_VISIBLE = False
    If OBJ_IS_VISIBLE.Class = "group" And P5 = False Then FUN_IS_VISIBLE = False
    If OBJ_IS_VISIBLE.Class = "computer" And P6 = False Then FUN_IS_VISIBLE = False
    If OBJ_IS_VISIBLE.Class = "printer" And P7 = False Then FUN_IS_VISIBLE = False
    If OBJ_IS_VISIBLE.Class = "volume" And P8 = False Then FUN_IS_VISIBLE = False
        
End Function

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.