Directory Services

Searching for Groups

This topic shows how to search for several types of groups using the DirectorySearcher.

The following code example shows how to search for all groups on the domain.

[C#]
DirectorySearcher src = new DirectorySearcher(ou,"(objectCategory=group)");
foreach(SearchResult res in src.FindAll())
{
Console.WriteLine(res.Path);
}

The following code example shows how to search for all security enabled groups. For this search, use COM Interop. It uses a bitwise search.

[C#]
int val = (int) ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_SECURITY_ENABLED;
string query = "(&(objectCategory=group)(groupType:1.2.840.113556.1.4.804:=" + val.ToString() + "))";
src.Filter = query;
foreach(SearchResult res in src.FindAll())
{
Console.WriteLine(res.Path);
}

The following code example shows how to search for all global domain groups, regardless of whether they are secured or non-secured. For this search, use COM Interop.

[C#]
val = (int) ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_GLOBAL_GROUP;
query = "(&(objectCategory=group)(groupType:1.2.840.113556.1.4.804:=" + val.ToString() + "))";
src.Filter = query;
foreach(SearchResult res in src.FindAll())
{
Console.WriteLine(res.Path);
}

The following code example shows how to search for all global domain, secured groups. For this search, use COM Interop.

[C#]
val = (int) (ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_GLOBAL_GROUP 
	| ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_SECURITY_ENABLED);
query = "(&(objectCategory=group)(groupType=" + val.ToString() + "))";
src.Filter = query;
foreach(SearchResult res in src.FindAll())
{
Console.WriteLine(res.Path);
}