Currently I have a gridview bound to a datatable which is populated with groups from the AD. I need to be able to add search functionality so users can type in part of a group name and have the results display only groups that fit their search criteria. Here's what I have so far.
<asp:TextBox ID="searchParam" runat="server"></asp:TextBox><asp:button ID="btnSearch" runat="server" Text="Search" />
<asp:GridView ID="dgSearchDLs" runat="server" AutoGenerateColumns="False" DataKeyNames="cn" DataSourceID="ObjectDataSource1">
<Columns>
<asp:BoundField DataField="cn" HeaderText="DL Name"/>
<asp:BoundField DataField="managedBy" HeaderText="Managed By"/>
<asp:BoundField DataField="info" HeaderText="Notes"/>
<asp:ButtonField ButtonType="Button" text="Add" HeaderText = "Select DL" CommandName="AddDL" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="getCOMDLs" TypeName="NewEmployee">
</asp:ObjectDataSource>
NewEmployee class:
Function getCOMDLs() As DataTable
Dim MySearchRoot As DirectoryEntry = New DirectoryEntry("path", "usr", "pwd")
Dim MyDirectorySearcher As New DirectorySearcher(MySearchRoot)
Dim strManagedBy As String
MyDirectorySearcher.Filter = ("(&(objectCategory=group)(|(name=dl*)))")
MyDirectorySearcher.SearchScope = SearchScope.Subtree
MyDirectorySearcher.PropertiesToLoad.Add("cn")
MyDirectorySearcher.PropertiesToLoad.Add("ManagedBy")
MyDirectorySearcher.PropertiesToLoad.Add("info")
MyDirectorySearcher.Sort.Direction = System.DirectoryServices.SortDirection.Ascending
MyDirectorySearcher.Sort.PropertyName = "cn"
Dim MySearchResult As SearchResultCollection = MyDirectorySearcher.FindAll()
Dim myTable As New DataTable("Results")
Dim colName As String
Dim i As Integer
For Each colName In MyDirectorySearcher.PropertiesToLoad
myTable.Columns.Add(colName, GetType(System.String))
Next
Dim result As SearchResult
For Each result In MySearchResult
Dim dr As DataRow = myTable.NewRow()
For Each colName In MyDirectorySearcher.PropertiesToLoad
If result.Properties.Contains(colName) Then
If colName = "ManagedBy" Then
strManagedBy = CStr(result.Properties(colName)(0))
i = strManagedBy.IndexOf(",")
strManagedBy = strManagedBy.Substring(3, i - 3)
dr(colName) = strManagedBy
Else
dr(colName) = CStr(result.Properties(colName)(0))
End If
Else
dr(colName) = ""
End If
Next
myTable.Rows.Add(dr)
Next
Return myTable
End Function