Search code examples
.netactive-directoryldapdirectoryservices

Using DirectorySearcher to query multiple OUs


I have the following code:

var directoryEntry = new DirectoryEntry(distributionListsListADSPath);
var directorySearcher = new DirectorySearcher(directoryEntry) 
          { SizeLimit = int.MaxValue, PageSize = int.MaxValue };
var result = directorySearcher.FindAll();

The problem is I want to search two seperate OUs.

So what I do is run through this twice, once where

private const string distributionListsListADSPath = 
"LDAP://OU=Distribution Lists,OU=Groups,DC=enron,DC=com";

and a second where it is

private const string distributionListsListADSPath = 
"LDAP://OU=Security Groups,OU=Groups,DC=enron,DC=com";

Ideally, I could do something like

private const string distributionListsListADSPath = 
"LDAP://OU=Distribution Lists | OU = Security Groups ,OU=Groups,DC=enron,DC=com";

Solution

  • Without knowing the hierarchy of your container, it is hard to form a proper answer.

    You may need to use Extensible Match which is described here: http://ldapwiki.willeke.com/wiki/ExtensibleMatch

    Good Luck -jim