Retrieve all AD objects within a OU

  • Thread starter =?iso-8859-1?q?R=E9my_Samulski?=
  • 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
 
W

Willy Denoyette [MVP]

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.
 
?

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

Thanks Willy for this quick answer and all your other answers. I admire
your activity in answering all our questions in these newsgroups!
 
?

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

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
 
?

=?iso-8859-1?q?R=E9my_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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top