Home All Groups Group Topic Archive Search About

How do I filter an Active Directory search to an OU (organizational unit)?

Author
13 Apr 2005 7:20 PM
JohnH.
Hi,

I need to be able to retrieve a list of the users in an OU.  The
following C# code works just fine and dumps out a list of all of the
user objects in all OUs in our active directory.  I have not been able
to figure out the filtering syntax to get this to just display the
users in an OU.  If anybody has any suggestions I'd be very
appreciative.  I've searched the online help, web, and news groups.
I must not be understanding some fundamental concept here because
nothing I have tried has worked.

using System;
using System.DirectoryServices;

namespace ADSearch
{ public class ADRead
  {
    static void Main(string[] args)
    {
      DirectoryEntry entry = new DirectoryEntry
                                            ("LDAP://jrcc.local");
      DirectorySearcher searcher = new DirectorySearcher(entry);
      searcher.Filter = "(&(objectClass=user)(objectCategory=person))";

      SearchResultCollection rc  = searcher.FindAll();
      for(int i=0; i<rc.Count; i++)
      {
        Console.WriteLine(rc[i].Path);
      }
      Console.WriteLine("Users Found: " + rc.Count.ToString());
    }
  }
}

Thanks,
John

Author
13 Apr 2005 7:38 PM
Joe Kaplan (MVP - ADSI)
Answered on other newsgroup.  Please try not to multi-post.

Joe K.

Show quoteHide quote
"JohnH." <j***@jrcc.net> wrote in message
news:1113420053.573364.223360@l41g2000cwc.googlegroups.com...
> Hi,
>
> I need to be able to retrieve a list of the users in an OU.  The
> following C# code works just fine and dumps out a list of all of the
> user objects in all OUs in our active directory.  I have not been able
> to figure out the filtering syntax to get this to just display the
> users in an OU.  If anybody has any suggestions I'd be very
> appreciative.  I've searched the online help, web, and news groups.
> I must not be understanding some fundamental concept here because
> nothing I have tried has worked.
>
> using System;
> using System.DirectoryServices;
>
> namespace ADSearch
> { public class ADRead
>  {
>    static void Main(string[] args)
>    {
>      DirectoryEntry entry = new DirectoryEntry
>                                            ("LDAP://jrcc.local");
>      DirectorySearcher searcher = new DirectorySearcher(entry);
>      searcher.Filter = "(&(objectClass=user)(objectCategory=person))";
>
>      SearchResultCollection rc  = searcher.FindAll();
>      for(int i=0; i<rc.Count; i++)
>      {
>        Console.WriteLine(rc[i].Path);
>      }
>      Console.WriteLine("Users Found: " + rc.Count.ToString());
>    }
>  }
> }
>
> Thanks,
> John
>
Author
13 Apr 2005 9:09 PM
Jeff Connelly
"Joe Kaplan (MVP - ADSI)" <joseph.e.kap***@removethis.accenture.com> wrote
in message news:%23OI5mBGQFHA.580@TK2MSFTNGP15.phx.gbl...
> Answered on other newsgroup.

What was the other newsgroup?
Author
14 Apr 2005 12:46 AM
Joe Kaplan (MVP - ADSI)
microsoft.public.adsi.general

Unfortunately, there is no newsgroup specific to .NET Directory Services
programming, so they tend to get scattered around to lots of different
groups.  This one is common, but the adsi.general group gets the most
traffic.

Your answer was really similar to mine FWIW.  :)

Joe K.

Show quoteHide quote
"Jeff Connelly" <nom***@thank.you> wrote in message
news:e3Kbt0GQFHA.904@tk2msftngp13.phx.gbl...
>
> "Joe Kaplan (MVP - ADSI)" <joseph.e.kap***@removethis.accenture.com> wrote
> in message news:%23OI5mBGQFHA.580@TK2MSFTNGP15.phx.gbl...
>> Answered on other newsgroup.
>
> What was the other newsgroup?
>
Author
14 Apr 2005 2:06 PM
Jeff Connelly
"Joe Kaplan (MVP - ADSI)" <joseph.e.kap***@removethis.accenture.com> wrote
in message news:%23TAKftIQFHA.1500@TK2MSFTNGP09.phx.gbl...
>
> Your answer was really similar to mine FWIW.  :)

That is what I was actually curious about :-)  Thanks.
Author
13 Apr 2005 7:45 PM
Jeff Connelly
Show quote Hide quote
"JohnH." <j***@jrcc.net> wrote in message
news:1113420053.573364.223360@l41g2000cwc.googlegroups.com...
> Hi,
>
> I need to be able to retrieve a list of the users in an OU.  The
> following C# code works just fine and dumps out a list of all of the
> user objects in all OUs in our active directory.  I have not been able
> to figure out the filtering syntax to get this to just display the
> users in an OU.  If anybody has any suggestions I'd be very
> appreciative.  I've searched the online help, web, and news groups.
> I must not be understanding some fundamental concept here because
> nothing I have tried has worked.
>
> using System;
> using System.DirectoryServices;
>
> namespace ADSearch
> { public class ADRead
>  {
>    static void Main(string[] args)
>    {
>      DirectoryEntry entry = new DirectoryEntry
>                                            ("LDAP://jrcc.local");

I think the easiest thing to do here would be to refine your LDAP string.
(LDAP syntax is confusing because several different looking syntaxes are
allowed - it's not consistent.  I'll show you one syntax that should work -
there are others.)  Let's say you had on the jrcc.local domain an OU called
MyBusiness (common), and then below that an OU called Users.  You would use
LDAP://OU=Users,OU=MyBusiness,DC=jrcc,DC=local

That sets your LDAP path to point to that node, your filter does the rest of
the work.  Although you might find you don't need your filter anymore.