I am trying to return all of the users in 2 OUs. The first OU (below) is IT Users within the HSD Users OU. This returns null every time, but I can get all of the users back with the following.
search.Filter = "(&(objectClass=user))";
I have tried many variations of the OU and DC in combination with no results.
string DomainPath = "LDAP://hs.domain.org";
DirectoryEntry searchRoot = new DirectoryEntry(DomainPath);
DirectorySearcher search = new DirectorySearcher(searchRoot);
//The following filter does work
//search.Filter = "(&(objectClass=user))";
search.Filter = string.Format("(&(objectClass=user)(OU=IT Users,OU=HSD Users,DC=hs,DC=domain,DC=org)");
search.PropertiesToLoad.Add("samaccountname");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("usergroup");
search.PropertiesToLoad.Add("displayname");//first name
search.PropertiesToLoad.Add("manager");
SearchResult result;
SearchResultCollection resultCol = search.FindAll();
if (resultCol != null)
{
for (int counter = 0; counter < resultCol.Count; counter++)
The LDAP filter you use is invalid, hence no records are returned. To locate objects in a particular container/OU or sub-tree, you need to set searchRoot to the path.
To find all entries contained directly in a specific container/OU, set the SearchScope to 1 (OneLevel) with the following constructor:
DirectorySearcher(DirectoryEntry, String, String[], SearchScope)
If you want to find all matching entries under a specific container/OU, then you can use the constructor above but use the specific container/OU as your searchRoot.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.