Get groups from OU

Sep 13, 2011 at 11:22 AM

Hello,

Is there a way to get all the Groups (not the users) available in the Organization Unit (OU) "Security Groups" ?

Thanks,

Feb 16, 2012 at 11:16 PM

I think this is basically what' you're looking for.

1) Create the Group class modeling from the example provided in this framework:

2) Create a basic Context class;

    sealed class MyContext : DirectoryContext
    {
        public MyContext(DirectoryEntry searchRoot)
            : base(searchRoot)
        {
        }
        [DirectorySearchOptions(SearchScope.Subtree)]
        public DirectorySource<ADGroup> Groups { get; set; }
    }

3) Query you data. Just sent the OU path to where you want to query.

	
MyContext ctx = new MyContext(new DirectoryEntry("LDAP://OU=Users,OU=TEST,DC=mydc,DC=com")); 

var groups = from x in ctx.Groups
		 where x.Name.StartsWith("Test")
		 select x;

foreach (var g in groups)
{
Console.WriteLine(g.Name);
}