Question

How can I prevent subcontainer objects in queries to a specific OU with subcontainers (child OU)?

To clarify, I don't want to include user objects in children OUs (subcontainers) in the result set.

Given something like the code on another stackoverflow post for example:

// create a principal object representation to describe
// what will be searched 
UserPrincipal user = new UserPrincipal(adPrincipalContext);

// define the properties of the search (this can use wildcards)
user.Enabled = false;
user.Name = "user*";

// create a principal searcher for running a search operation
PrincipalSearcher pS = new PrincipalSearcher();

// assign the query filter property for the principal object 
// you created
// you can also pass the user principal in the 
// PrincipalSearcher constructor
pS.QueryFilter = user;

// run the query
PrincipalSearchResult<Principal> results = pS.FindAll();

Console.WriteLine("Disabled accounts starting with a name of 'user':");
foreach (Principal result in results)
{
    Console.WriteLine("name: {0}", result.Name);
}

Thanks,

Victor

Was it helpful?

Solution

Unfortunately, this (and a few other) features aren't visible directly on the PrincipalSearcher class.

You need to "reach down" to the underlying DirectorySearcher to set options like this (and e.g. the page size):

DirectorySearcher ds = pS.GetUnderlyingSearcher() as DirectorySearcher;

if(ds != null)
{
   ds.SearchScope = SearchScope.Base;  // or SearchScope.OneLevel - your pick
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top