In case anyone stumbles across this, I still don't know why my original code
doesn't work, but I found another way around it, plus I've linked my LDAP
query results to a DataGrid. Here's the code:
<%@ Page Language="VB" Debug="true" CodeFile="Default.aspx.vb"
Inherits="_Default" %>
<%@ Import Namespace="System.DirectoryServices" %>
<%@ Import Namespace ="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="
http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script runat="server">
Sub Page_Load(ByVal Source As Object, ByVal E As EventArgs)
'Connect
Dim strLDAPPath As String = ""
strLDAPPath = "LDAP://dc=<domainname goes here>,dc=com"
Dim objSearchRoot As New DirectoryEntry(strLDAPPath)
Dim objDirectorySearcher As New DirectorySearcher(objSearchRoot)
'If search has been requested
If IsPostBack = True Then
'Restrict scope of search
objDirectorySearcher.PropertiesToLoad.Add("cn")
objDirectorySearcher.PropertiesToLoad.Add("displayName")
objDirectorySearcher.PropertiesToLoad.Add("department")
objDirectorySearcher.PropertiesToLoad.Add("givenName")
objDirectorySearcher.PropertiesToLoad.Add("mail")
objDirectorySearcher.PropertiesToLoad.Add("telephoneNumber")
objDirectorySearcher.PropertiesToLoad.Add("description")
objDirectorySearcher.PropertiesToLoad.Add("physicalDeliveryOfficeName")
objDirectorySearcher.PropertiesToLoad.Add("facsimileTelephoneNumber")
objDirectorySearcher.PropertiesToLoad.Add("objectClass")
objDirectorySearcher.PropertyNamesOnly = True
objDirectorySearcher.CacheResults = True
objDirectorySearcher.Filter = GetFilterString()
objDirectorySearcher.Sort = New SortOption("displayName",
DirectoryServices.SortDirection.Ascending)
'Get info from search box and search
Dim objSearchResultsCollection As SearchResultCollection
objSearchResultsCollection = objDirectorySearcher.FindAll()
If objSearchResultsCollection.Count > 0 Then
Dim objDataSet As New DataSet("ADDataSet")
Dim objGridTable As New DataTable
objGridTable = objDataSet.Tables.Add("objGridTable")
Dim objGridRow As DataRow
Dim objDataGrid As New DataGrid
With objGridTable
.Columns.Add("ID", Type.GetType("System.String"))
.Columns.Add("Name", Type.GetType("System.String"))
.Columns.Add("Title", Type.GetType("System.String"))
.Columns.Add("Dept", Type.GetType("System.String"))
.Columns.Add("Office", Type.GetType("System.String"))
.Columns.Add("Email", Type.GetType("System.String"))
.Columns.Add("Phone", Type.GetType("System.String"))
.Columns.Add("Fax", Type.GetType("System.String"))
End With
'Get the Results
For Each objSearchResult As SearchResult In
objDirectorySearcher.FindAll()
objGridRow = objGridTable.NewRow()
objGridRow("ID") =
objSearchResult.GetDirectoryEntry().Properties("cn").Value.ToString
If (objSearchResult.Properties.Contains("displayName"))
Then
objGridRow("Name") =
objSearchResult.GetDirectoryEntry().Properties("displayName").Value.ToString
Else
objGridRow("Name") = "---"
End If
If (objSearchResult.Properties.Contains("description"))
Then
objGridRow("Title") =
objSearchResult.GetDirectoryEntry().Properties("description").Value.ToString
Else
objGridRow("Title") = "---"
End If
If (objSearchResult.Properties.Contains("department"))
Then
objGridRow("Dept") =
objSearchResult.GetDirectoryEntry().Properties("department").Value.ToString
Else
objGridRow("Dept") = "---"
End If
If
(objSearchResult.Properties.Contains("physicalDeliveryOfficeName")) Then
objGridRow("Office") =
objSearchResult.GetDirectoryEntry().Properties("physicalDeliveryOfficeName").Value.ToString
Else
objGridRow("Office") = "---"
End If
If (objSearchResult.Properties.Contains("mail")) Then
objGridRow("Email") = "<a href='mailto:" &
objSearchResult.GetDirectoryEntry().Properties("mail").Value.ToString & _
"'>" &
objSearchResult.GetDirectoryEntry().Properties("mail").Value.ToString & "</a>"
Else
objGridRow("Email") = "---"
End If
If
(objSearchResult.Properties.Contains("telephoneNumber")) Then
objGridRow("Phone") =
objSearchResult.GetDirectoryEntry().Properties("telephoneNumber").Value.ToString
Else
objGridRow("Phone") = "---"
End If
If
(objSearchResult.Properties.Contains("facsimileTelephoneNumber")) Then
objGridRow("Fax") =
objSearchResult.GetDirectoryEntry().Properties("facsimileTelephoneNumber").Value.ToString
Else
objGridRow("Fax") = "---"
End If
objGridTable.Rows.Add(objGridRow)
Next objSearchResult
ctrlResults.DataSource = objGridTable
ctrlResults.DataBind()
End If
End If
'Refresh object cache on local computer and clear variables
objDirectorySearcher.CacheResults = False
objDirectorySearcher.Dispose()
End Sub
'Format Query
Public Function FormFilter(ByVal strCategory As String, _
ByVal strFName As String, ByVal strLName As String, _
ByVal strDept As String, ByVal strPhone As String) As String
Dim strResult As String
strResult = "(&(objectCategory=" & strCategory & _
")(displayName=" & strLName & "*)(givenName=" & strFName & _
"*)(department=" & strDept & "*)(telephoneNumber=" & _