I'm looking for the way to get list of the emails and groups of emails from the active directory. The list will be used to populate autocomplete textbox, the same as in Outlook. Have any of you done something like that in the past using Asp.Net MVC?
Asked
Active
Viewed 863 times
1 Answers
2
I made this in a project some time ago and here are few steps I think you need to take:
- Create a directory entry for your domain
- Create a directory searcher and initialize it with a filter
- Create a search collection searching with previous filter
- Iterate your search collection
- Get your properties for current iteration
- Get your email from property collection
Code example:
DirectoryEntry dir = new DirectoryEntry("LDAP://" + YourDomain, LoginUsername, LoginPassword);
DirectorySearcher search = new DirectorySearcher(dir);
search.Filter = "(&(objectClass=user)(objectCategory=person))";
SearchResultCollection searchResultCollection = search.FindAll();
if (searchResultCollection != null)
{
for (int i = 0; i < searchResultCollection.Count; i++)
{
SearchResult crt= searchResultCollection[i];
PropertyCollection properties= crt.GetDirectoryEntry().Properties;
// get email from properties["email"].Value
}
}