Retrieve all AD objects within a OU

  • Thread starter Thread starter =?iso-8859-1?q?R=E9my_Samulski?=
  • Start date Start date
?

=?iso-8859-1?q?R=E9my_Samulski?=

Dear readers,

How can I obtain all AD Objects within a OU? I can find all AD Objects
in the root of my Active Directory when I use the DirectorySearcher and
passing the DirectoryEntry I find using the defaultNamingContext
property of the RootDSE DirectoryEntry. However when I try to pass
another DirectoryEntry as SearchRoot I trigger an error. Does anyone
know how I can use the DirectorySearcher (or something else) to
retrieve all users from a particular OU?

Many Thanks,
Remy Samulski
 
Rémy Samulski said:
Dear readers,

How can I obtain all AD Objects within a OU? I can find all AD Objects
in the root of my Active Directory when I use the DirectorySearcher and
passing the DirectoryEntry I find using the defaultNamingContext
property of the RootDSE DirectoryEntry. However when I try to pass
another DirectoryEntry as SearchRoot I trigger an error. Does anyone
know how I can use the DirectorySearcher (or something else) to
retrieve all users from a particular OU?

Many Thanks,
Remy Samulski

Set the SearchRoot to the root entry of the OU.
Here's a sample...

using (DirectoryEntry de = new
DirectoryEntry("LDAP://yourDomain/ou=someou,dc=...;dc=....;dc=...."))
{
DirectorySearcher src = new DirectorySearcher();
// retrieve only cn and distinguishedname properties
string[] props = {"cn", "distinguishedname"};
src.PropertiesToLoad.AddRange(props);
src.SearchRoot = de;
src.SearchScope = SearchScope.Subtree;
// search only object category user
src.Filter = "(objectCategory=user)";
// use a paged search
src.PageSize = 500;
SearchResultCollection res = src.FindAll();
foreach(SearchResult sc in res)
{
// show only cn property
foreach(string myCollection in sc.Properties["cn"])
Console.WriteLine(myCollection);
}
}


Willy.
 
Thanks Willy for this quick answer and all your other answers. I admire
your activity in answering all our questions in these newsgroups!
 
Willy said:
Set the SearchRoot to the root entry of the OU.
Here's a sample...

using (DirectoryEntry de = new
DirectoryEntry("LDAP://yourDomain/ou=someou,dc=...;dc=....;dc=...."))
{
DirectorySearcher src = new DirectorySearcher();
// retrieve only cn and distinguishedname properties
string[] props = {"cn", "distinguishedname"};
src.PropertiesToLoad.AddRange(props);
src.SearchRoot = de;
src.SearchScope = SearchScope.Subtree;
// search only object category user
src.Filter = "(objectCategory=user)";
// use a paged search
src.PageSize = 500;
SearchResultCollection res = src.FindAll();
foreach(SearchResult sc in res)
{
// show only cn property
foreach(string myCollection in sc.Properties["cn"])
Console.WriteLine(myCollection);
}
}


Willy.

Dear Willy,

I still receive following error message in my program:

An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in
system.directoryservices.dll

Additional information: The specified directory service attribute or
value does not exist

Do you have a clue?

Many Thanks,
Remy Samulski
 
Found the problem, entered empty credentials. Although this works for
the root it didn't work for the sub OU's. Sorry for the postings and
thx again!
 
Back
Top