Searching the GAL
When you are developing internal applications you often need to access company infromation, such as employee details. There are two common places to search for this information, LDAP (Active Directory) or the Global Account List (GAL). This will be a simple example of searching the GAL to get you started.
Both options require the use of DirectoryServices to perform the data reqeust. So the first thing you should do is add a reference to the System.DirectoryServices component in your project. You can do this by Right-Clicking your project in the Solution window and choosing the 'Add a Reference' option on the context menu. Search the list of namespaces to find the System.DirectoryServices componet and add it to your project.
You will eather need to add a global reference to yuor application or the page you are need DirectorieServices objects:Imports System.DirectoryServicesActually performing a search is pretty simple, you may need to know the potential property names to filter by, so check with your system admins in you need some help. This is an example to search by last name. You need to create an instance of the DirectorySearcher. Next you need to create a DirectoryEntry, you should add the Path, which you can just use the Root.
Imports System.DirectoryServicesActually performing a search is pretty simple, you may need to know the potential property names to filter by, so check with your system admins in you need some help. This is an example to search by last name. You need to create an instance of the DirectorySearcher. Next you need to create a DirectoryEntry, you should add the Path, which you can just use the Root.
Actually performing a search is pretty simple, you may need to know the potential property names to filter by, so check with your system admins in you need some help. This is an example to search by last name. You need to create an instance of the DirectorySearcher. Next you need to create a DirectoryEntry, you should add the Path, which you can just use the Root.
Dim myDirectorySearcher As New DirectorySearcherDim mySearchRoot As New DirectoryEntry(myDirectorySearcher.SearchRoot.Path)You next need to specify the filter to limit your query, this is a simple way to search for a person. More complicated queries can be done and require a little more study of the syntax, remember I just want to make this a simple example. You then can limit the Scope, this example limits it to the Subtree.
Dim mySearchRoot As New DirectoryEntry(myDirectorySearcher.SearchRoot.Path)You next need to specify the filter to limit your query, this is a simple way to search for a person. More complicated queries can be done and require a little more study of the syntax, remember I just want to make this a simple example. You then can limit the Scope, this example limits it to the Subtree.
You next need to specify the filter to limit your query, this is a simple way to search for a person. More complicated queries can be done and require a little more study of the syntax, remember I just want to make this a simple example. You then can limit the Scope, this example limits it to the Subtree.
myDirectorySearcher.Filter = String.Format("(& (objectClass=user)(sn={0}*))", slastName)myDirectorySearcher.SearchScope = System.DirectoryServices.SearchScope.Subtree
Next you actually perform the search with the FindAll (note: you can also use FindOne method to get just one). It returns a SearchResultCollection you can interate through
myDirectorySearcher.SearchScope = System.DirectoryServices.SearchScope.Subtree
Next you actually perform the search with the FindAll (note: you can also use FindOne method to get just one). It returns a SearchResultCollection you can interate through
Dim mySearchResultS As SearchResultCollection = myDirectorySearcher.FindAll
You can then Bind the SearchResultCollection to a data control, I like Repeaters :). If you only get one object back, then you can access that item directly.
Private Sub SearchEmployees(ByVal slastName As String)Dim myDirectorySearcher As New DirectorySearcherDim mySearchRoot As New DirectoryEntry(myDirectorySearcher.SearchRoot.Path)myDirectorySearcher.Filter = String.Format("(& (objectClass=user)(sn={0}*))", slastName)myDirectorySearcher.SearchScope = System.DirectoryServices.SearchScope.Subtree
Dim mySearchResultS As SearchResultCollection = myDirectorySearcher.FindAllIf mySearchResultS.Count = 0 Then
Sub SearchEmployees(ByVal slastName As String)Dim myDirectorySearcher As New DirectorySearcherDim mySearchRoot As New DirectoryEntry(myDirectorySearcher.SearchRoot.Path)myDirectorySearcher.Filter = String.Format("(& (objectClass=user)(sn={0}*))", slastName)myDirectorySearcher.SearchScope = System.DirectoryServices.SearchScope.Subtree
Dim mySearchResultS As SearchResultCollection = myDirectorySearcher.FindAllIf mySearchResultS.Count = 0 Then
Dim myDirectorySearcher As New DirectorySearcherDim mySearchRoot As New DirectoryEntry(myDirectorySearcher.SearchRoot.Path)myDirectorySearcher.Filter = String.Format("(& (objectClass=user)(sn={0}*))", slastName)myDirectorySearcher.SearchScope = System.DirectoryServices.SearchScope.Subtree
Dim mySearchResultS As SearchResultCollection = myDirectorySearcher.FindAllIf mySearchResultS.Count = 0 Then
Dim mySearchRoot As New DirectoryEntry(myDirectorySearcher.SearchRoot.Path)myDirectorySearcher.Filter = String.Format("(& (objectClass=user)(sn={0}*))", slastName)myDirectorySearcher.SearchScope = System.DirectoryServices.SearchScope.Subtree
Dim mySearchResultS As SearchResultCollection = myDirectorySearcher.FindAllIf mySearchResultS.Count = 0 Then
String.Format("(& (objectClass=user)(sn={0}*))", slastName)myDirectorySearcher.SearchScope = System.DirectoryServices.SearchScope.Subtree
Dim mySearchResultS As SearchResultCollection = myDirectorySearcher.FindAllIf mySearchResultS.Count = 0 Then
Dim mySearchResultS As SearchResultCollection = myDirectorySearcher.FindAllIf mySearchResultS.Count = 0 Then
If mySearchResultS.Count = 0 ThenltlErrMsg.Text = "Sorry there are no employees matching your criteria, please try again."
Exit Sub
Exit SubEnd If
End IfIf mySearchResultS.Count > 1 Then
If mySearchResultS.Count > 1 Then'rptRequestee is a DataRepeater
rptRequestee.DataSource = mySearchResultS
rptRequestee.DataBind()
Else
Else'Now you can do something with the Property
mySearchResultS(0).Properties
End If
End IfEnd Sub
I know this is a very simple example and I glossed over what can be a pretty complex topic, so stay tuned for more examples...