Re: LDAP query to DataGrid

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>


.



Relevant Pages

  • Re: DirectoryService and get all groups
    ... Dim dsGroups As New DirectorySearcher(dirEnt, ... Dim srGroupsCol As SearchResultCollection = dsGroups.FindAll ... Dim srGroups As SearchResult ... properties of the objectCategory of group. ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: LDAP and Organization group
    ... DirectoryEntry obj and pased the path, as well as username and pw to it. ... > specify credentials with your DirectoryEntry unless you want your password ... >> Dim searchResult As SearchResult ... >> Dim mySearchResultColl As SearchResultCollection ...
    (microsoft.public.dotnet.security)
  • Re: How can I list all the users in a particular security group with ASP.NET?
    ... Sub Page_Load ... Dim searcher As New DirectorySearcher ... Dim result As SearchResult, strDept As String, strOldDept As String ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: LDAP, Email
    ... SearchResult and then you go and create another DirectoryEntry just to read ... strEmail = DirectCast, String) ... Dim oDS As New DirectorySearcher ...
    (microsoft.public.dotnet.security)
  • Project Error
    ... Private Declare Sub Sleep Lib "Kernel32" ... Dim strDataSrc As String ...
    (microsoft.public.vb.bugs)