I'm trying to query Active Directory and return an IEnumerable but with properties like Title and EmailAddress, which aren't shown doing PrincipalSearcher.Findll(). If I use PrincipalSearcher.FindOne(), it has much more properties (still not Title, though) so I'm trying to figure out what I'm doing differently or how to get the info I need out. I've worn out Google trying to find more info and it seems like UserPrincipal.GetUnderlyingObject() might be the ticket but I'm not understanding how to involve that into a foreach loop in order to populate it into the List.
public class ADUser
{
public string SamAccountName { get; set; }
public string DisplayName { get; set; }
public string Title { get; set; }
public IEnumerable<ADUser> Get(string username)
{
var users = new List<ADUser>();
var principalContext = new PrincipalContext(ContextType.Domain, "domain.com");
var userPrincipal = new UserPrincipal(principalContext)
{
SamAccountName = username
};
var principalSearcher = new PrincipalSearcher(userPrincipal);
foreach (var user in principalSearcher.FindAll())
{
users.Add(new ADUser
{
SamAccountName = user.SamAccountName,
DisplayName = user.DisplayName,
//Title = user.Title //Won't work, no Title property
});
}
return users;
}
}
This works but only returns a fraction of the properties .FindOne() does, but if I do FindOne(), I won't be able to search for partial usernames, such as "jsm" returning "John Smith" and "James Smoth".