Question : How to retrive OU Properties in an Active Directory using VB.NET

Hi experts, i have a task to show the list of OU's in a OU in a certain domain in a listbox , After displaying the OU's in the listbox, when i select an OU in the list box i have to see the Containers, User's and Groups that are present in that perticular OU in another three different ListBox's.Please help me

Answer : How to retrive OU Properties in an Active Directory using VB.NET


Actually, that said, I would change it a bit.

You're using the "GetDirectoryEntry()" method unnecessarily. It's not the end of the world, but it does make any kind of project like this scale badly because it's a lot of work.

You'd already loaded name into the property set, so rather than heading back to AD to get the name we can use the entry it already asked for. Changed below :)

Chris
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
Imports System.Data
Imports System.DirectoryServices
Public Class Form1
    Dim objpath As DirectoryEntry
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
 
            objpath = New DirectoryEntry("LDAP://OU=Accounts,DC=Test,DC=COM")
 
            Dim oSearcher As DirectorySearcher = New DirectorySearcher(objpath)
            Dim oResults As SearchResultCollection
            Dim oResult As SearchResult
 
            oSearcher.PropertiesToLoad.Add("name")
            oSearcher.PropertiesToLoad.Add("cn")
 
            ' Set the search scope
            oSearcher.SearchScope = "OneLevel"
 
            oResults = oSearcher.FindAll
 
            For Each oResult In oResults
 
                ' CN will never be "", it's a mandatory attribute in AD
                ' If oResult.Properties("cn")(0) = "" Then
 
                ' Properties are stored in a collection, single value properties like name, 
                ' and CN are stored at index (0) in the collection.
 
                MessageBox.Show(oResult.Properties("name")(0))
                ListBox1.Items.Add(oResult.Properties("name")(0))
 
                ' End If
 
            Next
        
        Catch ex As Exception
            MessageBox.Show(ex.Message + " " + ex.StackTrace)
        End Try
        
    End Sub
End Class
Random Solutions  
 
programming4us programming4us