C# Question

Get email from Active Directory result

I have the following function, but I'm unable to get email and others stuff like phone number. What can I do to get that?

public static List<AdEmployees> CreateEmployeeList(string department)
{
List<AdEmployees> employees = new List<AdEmployees>();

PrincipalContext pricipalContext = new PrincipalContext(ContextType.Domain);
GroupPrincipal group = GroupPrincipal.FindByIdentity(pricipalContext, department);

if (group != null)
{
foreach (Principal principal in group.Members)
{
employees.Add(new AdEmployees() { name = principal.Name });
}
}
return employees;
}


string department is a given group in ad ( could be "guest" or "admin" or something else )

Answer

try this:

PrincipalContext pricipalContext = new PrincipalContext(ContextType.Domain);
        GroupPrincipal group = GroupPrincipal.FindByIdentity(pricipalContext, department);

        if (group != null)
        {
            foreach (Principal principal in group.Members)
            {
                UserPrincipal user = UserPrincipal.FindByIdentity(pricipalContext, principal.Name);
                employees.Add(new AdEmployees() { name = user.Name });
            }
        }

Some Ad object like "Office" is not found this way. then you can try

UserPrincipal tu = principal as UserPrincipal;
DirectoryEntry de = tu.GetUnderlyingObject() as DirectoryEntry;
var store = de.InvokeGet("physicalDeliveryOfficeName").ToString()