Re: LDAP query to DataGrid
- From: "Dave" <ddd@xxxxxx>
- Date: Mon, 11 Jul 2005 03:13:40 +0100
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
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"Dave" <ddd@xxxxxx> wrote in message
news:DO6dnSffruudn0zfRVnytQ@xxxxxxxxxxxxxxx
> 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
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
.
- References:
- LDAP query to DataGrid
- From: Dave
- LDAP query to DataGrid
- Prev by Date: Re: Hosting ASP .Net pages
- Next by Date: Re: Hosting ASP .Net pages
- Previous by thread: LDAP query to DataGrid
- Next by thread: Access to remote application and bottleneck
- Index(es):
Relevant Pages
|