Reputation: 55
I have an Active Directory group with a name like Stack Over Flow IT
. Need to find AD group email like [email protected]
. No need to find AD user list.
How to find AD group email address?
Or how to find AD group name using AD group email address?
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");
I'm unable to find Active Directory group email address using the above group
instance code.
Upvotes: 2
Views: 1511
Reputation: 3892
You can do the following:
PropertyValueCollection email = ((DirectoryEntry) group.GetUnderlyingObject()).Properties["mail"];
If you have RSAT available you can validate your code (in powershell) with:
get-adgroup -Identity "Stack Over Flow IT" -properties mail | select name,mail | sort mail
Here is the reverse way for completeness:
// replace stuff inside [] to match your environment
DirectoryEntry root = new DirectoryEntry("LDAP://dc=[YOUR DC]", [username], [password], AuthenticationTypes.Secure);
DirectorySearcher groupSearcher = new DirectorySearcher(root);
groupSearcher.Filter = "([email protected])";
groupSearcher.PropertiesToLoad.Add("name");
foreach (SearchResult groupSr in groupSearcher.FindAll())
{
ResultPropertyValueCollection groupName = groupSr.Properties["name"];
// do something with finding
}
Upvotes: 4