Directory Services

Getting Search Results

Each result returned by DirectorySearcher can be retrieved as a SearchResult object. Each SearchResult object contains a single result and its associated properties.

The properties returned in each SearchResult are contained in a ResultPropertyCollection object. The values for these properties are contained in the ResultPropertyValueCollection object.

The FindOne method will return a single SearchResult. The following example shows how to use FindOne to obtain a single SearchResult and retrieve the values for all of its properties from the ResultPropertyValueCollection.

[Visual Basic .NET]
' Bind to a specific user.
Dim entry As New DirectoryEntry("LDAP://CN=User Name,CN=users,DC=fabrikam,DC=com")
' Create a DirectorySearcher object.
Dim mySearcher As New DirectorySearcher(entry)
' Use the FindOne method to find the object, which in this case, is the user
' indicated by User Name and assign it to a SearchResult.
Dim ResEnt As SearchResult = mySearcher.FindOne()
' Assign a property name to propKey.
Dim propKey As String
' Assign a property name to propKey.
For Each propKey In  ResEnt.Properties.PropertyNames
	' Assign the property value to prop.
	Dim prop As [Object]
	' Assign the property value to prop.
		For Each prop In  ResEnt.Properties(propKey)
			 ' Handle results. Be aware that the following WriteLine
			 ' only returns readable results for properties that are strings.
			 Console.WriteLine("{0}:{1}", propKey, prop.ToString())
		Next prop
	Next propKey
' Add error handling.
[C#]
// Bind to a specific user.
DirectoryEntry entry = new DirectoryEntry("LDAP://CN=User Name,CN=users,DC=fabrikam,DC=com");
// Create a DirectorySearcher object.
DirectorySearcher mySearcher = new DirectorySearcher(entry);
// Use the FindOne method to find the object, which in this case, is the user
// indicated by User Name and assign it to a SearchResult.
SearchResult resEnt = mySearcher.FindOne();
//
foreach(string propKey in resEnt.Properties.PropertyNames)
{
	// Assign a property name to propKey.
	foreach(string propKey in ResEnt.Properties.PropertyNames)
	{
		// Retrieve the value assigned to that property name 
		// in the ResultPropertyValueCollection. 
		ResultPropertyValueCollection valcol = resEnt1.Properties[propKey];   
		// Assign the property value to prop.
		foreach(Object prop in valcol)
		{
			// Handle results. Be aware that the following WriteLine
			// only returns readable results on properties that are strings.
			Console.WriteLine("{0}:{1}", propKey, prop.ToString());
	}
}
}
// Add error handling.

When the search retrieves one or more results, use the FindAll method. This method returns a collection of SearchResult objects contained in the SearchResultCollection object. Use a Foreach statement to iterate through the SearchResultCollection and obtain data from individual SearchResult objects.

The following code example uses a wild card search filter and the FindAll method to find all users that have "test" as part of the user name. All the property values associated to each object in the result set are also retrieved from the ResultPropertyValueCollection object.

[Visual Basic .NET]
' Put code in Try block to capture errors.
Try
' Bind to the users container.
Dim entry As New DirectoryEntry("LDAP://CN=users,DC=fabrikam,DC=com")
' Create a DirectorySearcher object.
Dim mySearcher As New DirectorySearcher(entry)
' Set a filter for users with the name test.
mySearcher.Filter = "(&(objectClass=user)(anr=test*))"
' Use the FindAll method to return objects to a SearchResultCollection.
Dim ResEnt As SearchResultCollection = mySearcher.FindAll()
' Iterate through each SearchResult in the SearchResultCollection.
Dim resEnt1 As SearchResult
	For Each resEnt1 In ResEnt
		' Iterate through each property name in each SearchResult.
		Dim propKey As String
		For Each propKey In resEnt1.Properties.PropertyNames	 
			' Retrieve the value assigned to that property name 
			' in the ResultPropertyValueCollection.
			Dim valcol As ResultPropertyValueCollection = resEnt1.Properties(propKey)
			' Iterate through values for each property name in each SearchResult.
			Dim prop As [Object]
			For Each prop In valcol
				' Handle results. Be aware that the following WriteLine
				' only returns readable results on properties that are strings.
				Console.WriteLine("{0}:{1}", propKey, prop.ToString())
			 Next prop
		Next propKey
	Next resEnt1
Catch
' Add error handling.
[C#]
// Bind to the users container.
DirectoryEntry entry = new DirectoryEntry("LDAP://CN=users,DC=fabrikam,DC=com");
// Create a DirectorySearcher object.
DirectorySearcher mySearcher = new DirectorySearcher(entry);
// Set a filter for users with the name test.
mySearcher.Filter = "(&(objectClass=user)(anr=test*))";
// Use the FindAll method to return objects to a SearchResultCollection.
SearchResultCollection ResEnt = mySearcher.FindAll();
// Iterate through each SearchResult in the SearchResultCollection.
foreach(SearchResult resEnt1 in ResEnt)
{
	// Iterate through each property name in each SearchResult.
	foreach(string propKey in resEnt1.Properties.PropertyNames)
	{
		// Retrieve the value assigned to that property name 
		// in the ResultPropertyValueCollection.
		ResultPropertyValueCollection valcol = resEnt1.Properties[propKey];
		// Iterate through values for each property name in each SearchResult.
		foreach(Object prop in valcol)
		{
			// Handle results. Be aware that the following WriteLine
			// only returns readable results for properties that are strings.
			Console.WriteLine("{0}:{1}", propKey, prop.ToString());
	}
}
}
Catch
{
// Add error handling.
}

To retrieve only specific property values for the objects returned in the result set for the previous code example, you can replace the inner two Foreach statements with statements that provide the names of the properties that you want, as shown in the following statements.

[Visual Basic .NET]
Console.WriteLine(resEnt1.Properties("cn")(0))
Console.WriteLine(resEnt1.Properties("objectClass")(1))
[C#]
Console.WriteLine(resEnt1.Properties["cn"][0]);
Console.WriteLine(resEnt1.Properties["objectClass"][1]);

In these statements, the specific property is named and indexed. If the properties contain multiple values, you need to enumerate the values. For more information about enumerating values for properties, see Reading Properties with Multiple Values.

Another option provided by the ResultPropertyValueCollection object is the Item property, which retrieves a value at a specified index position in properties that contain multiple values. The Item keyword is used in Visual Basic .NET, but in C#, the implementation of Item is an array that contains the index position for the value to retrieve. The following code example shows how to use Item.

[Visual Basic .NET]
Try
' Bind to a specific user.
Dim entry As New DirectoryEntry("LDAP://CN=User Name,CN=users,DC=fabrikam,DC=com")
' Create a DirectorySearcher object.
Dim mySearcher As New DirectorySearcher(entry)
' Use the FindOne method to find the object, which in this case, is the user
' indicated by User Name and assign it to a SearchResult.
Dim resEnt As SearchResult = mySearcher.FindOne()
' Create a ResultPropertyValueCollection object to get the values for the 
' memberOf attribute for this user.
Dim valcol As ResultPropertyValueCollection = resEnt.Properties("memberOf")
' Write the value contained in index position 5 in the memberOf attribute, using Item.
Console.WriteLine("{0}", valcol.Item(5))
Catch NotSupp As Exception
' Add error handling.
[C#]
Try
// Bind to a specific user.
DirectoryEntry entry = new DirectoryEntry("LDAP://CN=User Name,CN=users,DC=fabrikam,DC=com");
// Create a DirectorySearcher object.
DirectorySearcher mySearcher = new DirectorySearcher(entry);
// Use the FindOne method to find the object, which in this case, is the user
// indicated by User Name and assign it to a SearchResult.
SearchResult resEnt = mySearcher.FindOne();
// Create a ResultPropertyValueCollection object to get the values for the 
// memberOf attribute for this user.
ResultPropertyValueCollection valcol = resEnt.Properties["memberOf"];
{
	// Write the value contained in index position 5 in the memberOf attribute.
	Console.WriteLine("{0}",valcol[5].ToString());
}
Catch
{
// Add error handling.
}

For search results, System.DirectoryServices does not support the following return types: