LDAP query to DataGrid


D

Dave

Could someone fix this for me please. The last bit i cant figure out is the
last line in the sub.

Results.SetDataBinding(myTable.DefaultView, "")

Thanks

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Imports System.DirectoryServices

Partial Class _Default

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

Dim strADPath As String

strADPath = "domain.com"

Dim rootEntry As DirectoryEntry = New DirectoryEntry("LDAP://" & strADPath,
"Administrator", "password")

Dim searcher As New DirectorySearcher(rootEntry)

searcher.PropertiesToLoad.Add("cn")

searcher.PropertiesToLoad.Add("email")

searcher.PropertiesToLoad.Add("adsPath")

'searcher.PropertiesToLoad.AddRange(New String() {"cn", "mail"})

'would also work and saves you some code

searcher.Filter = ("(&(objectCategory=person)(objectClass=user))")

Dim myTable As New Data.DataTable("Results")

Dim colName As String

For Each colName In searcher.PropertiesToLoad

myTable.Columns.Add(colName, GetType(System.String))

Next

Dim queryResults As SearchResultCollection

queryResults = searcher.FindAll()

Dim result As SearchResult

For Each result In queryResults

Dim dr As Data.DataRow = myTable.NewRow()

For Each colName In searcher.PropertiesToLoad

If result.Properties.Contains(colName) Then

dr(colName) = CStr(result.Properties(colName)(0))

Else

dr(colName) = ""

End If

Next

myTable.Rows.Add(dr)

Next

Results.SetDataBinding(myTable.DefaultView, "")

End Sub

End Class

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Ad

Advertisements

D

Dave

Just incase if anyone is interested... Seems not. ;) I got it sorted at
last. I Started from scratch..... again! With the help of numerous resources
from MSDN no less. For the any noobs out there... (I was one last week,
still am!), Drag and drop a GridView or DataGrid Control to the HTML source
page. Then just copy and past this code below into the Code Behind aspx.vb
page. Thats it your done!

Hmmm.... Q. How do I populate a Treeview Control with Users and their
'managers'.
A. DSML over SOAP...
This could be interesting, Any suggestions welcome...
Dave

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Imports System.Data

'You need to add a Reference to this.....

Imports System.DirectoryServices

Partial Class _Default

Inherits System.Web.UI.Page

Function CreateDataSource() As ICollection

Dim dt As New DataTable()

Dim dr As DataRow

dt.Columns.Add(New DataColumn("CommonName", GetType(String)))

dt.Columns.Add(New DataColumn("Email Address", GetType(String)))

dt.Columns.Add(New DataColumn("CompanyName", GetType(String)))

dt.Columns.Add(New DataColumn("sAMAccountName", GetType(String)))

'If you want to search a particular OU dont forget the LDAP structure reads
backwards to the root.

'"LDAP://OU=Child,OU=Parent,OU=GrandParent,DC=domain,DC=com, "UserName",
"password")

Dim root As New
DirectoryServices.DirectoryEntry("LDAP://OU=User,DC=domain,DC=com",
"UserName", "password")

Dim rootSearch As New DirectorySearcher(root)

Dim SearchResult As SearchResult

Dim results As SearchResultCollection

rootSearch.PropertiesToLoad.AddRange(New String() {"cn", "mail", "company",
"sAMAccountName"})

rootSearch.Filter = "(&(objectCategory=Person)(objectClass=user))"

results = rootSearch.FindAll

For Each SearchResult In results

Try

dr = dt.NewRow()

'If the property contains no value it wont be included in the search results

' So use the If..... End If statements

If SearchResult.Properties.Contains("CN") Then

dr(0) = SearchResult.Properties("CN").Item(0)

End If

If SearchResult.Properties.Contains("mail") Then

dr(1) = SearchResult.Properties("mail").Item(0)

Else

dr(1) = "No E-Mail Address"

End If

If SearchResult.Properties.Contains("company") Then

dr(2) = SearchResult.Properties("company").Item(0)

End If

If SearchResult.Properties.Contains("sAMAccountName") Then

dr(3) = SearchResult.Properties("sAMAccountName").Item(0)

End If

dt.Rows.Add(dr)

Catch ex As Exception

Dim debug As String = ex.Message

End Try

Next

Dim dv As New DataView(dt)

Return dv

End Function 'CreateDataSource

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

If Not IsPostBack Then

' Load this data only once.

GridView1.DataSource = CreateDataSource()

GridView1.DataBind()

End If

End Sub

End Class

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Ad

Advertisements

Joined
Mar 6, 2009
Messages
1
Reaction score
0
Thanks for the help

I know this is an old post but it helped me today and I want to thank you. While I have many years of IT experience, this is my first time working with LDAP and I do not have much object oriented experience so everything is a struggle. I modified your code and used it in a Click event and with a little tiral and error got it working perfectly. Again, a BIG Thank You!!!

Imports System.DirectoryServices
Imports System.Data.SqlClient
Imports System.Data
Imports ActiveDirectory
Imports Functions

Partial Class Corporate_Directory
Inherits System.Web.UI.Page

Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click

Dim dt As New DataTable()
Dim dr As DataRow = Nothing
dt.Columns.Add(New DataColumn("Name", GetType(String)))
dt.Columns.Add(New DataColumn("Title", GetType(String)))
dt.Columns.Add(New DataColumn("Office", GetType(String)))
dt.Columns.Add(New DataColumn("Phone", GetType(String)))

' Authenticate to Active Directory
Dim ldapPath As String = ConfigurationManager.AppSettings("LDAP")
Dim entry As New DirectoryEntry(ldapPath)

Try
Dim native As Object = entry.NativeObject
Catch ex As Exception
Return
Exit Sub
End Try
Try
'Find all Matches
Dim search As DirectorySearcher = New DirectorySearcher(entry)
search.Filter = "(displayname= *" & txtFName.Text & "*)"
search.PropertiesToLoad.Add("displayname")
search.PropertiesToLoad.Add("title")
search.PropertiesToLoad.Add("telephoneNumber")
search.PropertiesToLoad.Add("physicalDeliveryOfficeName")

Dim results As SearchResultCollection = search.FindAll

If Not (results Is Nothing) Then

For Each result As SearchResult In results
dr = dt.NewRow()

If result.Properties.Contains("DisplayName") Then
dr(0) = result.Properties("DisplayName").Item(0)
End If

If result.Properties.Contains("title") Then
dr(1) = result.Properties("title").Item(0)
Else
dr(1) = "No Title"
End If

If result.Properties.Contains("physicalDeliveryOfficeName") Then
dr(2) = result.Properties("physicalDeliveryOfficeName").Item(0)
Else
dr(2) = "No Office"
End If

If result.Properties.Contains("telephoneNumber") Then
dr(3) = result.Properties("telephoneNumber").Item(0)
Else
dr(3) = "No Phone"
End If
dt.Rows.Add(dr)

Next

End If

Catch ex As Exception
Return
End Try

GridView1.DataSource = dt

GridView1.DataBind()

End Sub

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top