I know this is not a Moss post and i know that in Moss we trust, but it is a nice code to have.
Recently i needed to get all related AD groups per user, so if a user A is in group B and group B is in group C, i will get a collection like so { B,C } for A.
To do so i used the following code:
/// ldapPath - Ldap://...
/// distinguishedName - The user location (CN=AAA, OU=BBB,OU=CCC,DC=DDD )
///resultField - The property field value (DisplyName, Mail)
public static void GetAllRelatedGroupsByEntry(string ldapPath, string distinguishedName,
ref List<string> groups, string resultField)
{
SearchResultCollection results = null;
using (DirectoryEntry entry = new DirectoryEntry(ldapPath))
{
using (DirectorySearcher search = new DirectorySearcher(entry))
{
search.Filter = "(&(objectClass=Group)(objectCategory=Group)(member=" + distinguishedName + "))";
results = search.FindAll();
if (results != null)
{
foreach (SearchResult result in results)
{
for (int i = 0; i <>
{
groups.Add(result.Properties[resultField][i].ToString());
GetAllRelatedGroupsByEntry(ldapPath,
result.Properties["DistinguishedName"][i].ToString(),
ref groups, resultField);
}
}
}
}
}
}