Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Feb 10, 2013 10:17 PM by Rion Williams
Feb 04, 2013 10:56 PM|LINK
I have the following code that returns a user's first and last name.
How do I change the code to retrieve all users and then filter users instead of hard coding a user name?
DirectorySearcher searcher = new System.DirectoryServices.DirectorySearcher("(samaccountname=johndoe)");
System.DirectoryServices.SearchResultCollection userlist= searcher.FindAll();
Feb 04, 2013 11:09 PM|LINK
This Microsoft Example using DirectorySearcher uses the following to get all of the users within a domain :
// Bind to the users container.
DirectoryEntry entry = new DirectoryEntry("LDAP://CN=users,DC=fabrikam,DC=com");
// Create a DirectorySearcher object.
DirectorySearcher mySearcher = new DirectorySearcher(entry);
// Create a SearchResultCollection object to hold a collection of SearchResults
// returned by the FindAll method.
SearchResultCollection result = mySearcher.FindAll();
This StackOverflow discussion might be helpful as well, as it discusses the same question but provides a bit of explaination about using and querying
the Active Directory.
Feb 04, 2013 11:28 PM|LINK
The string s won't return any values
for (int i = 0; i < result.Count; i++)
string s = result[i].Properties["displayname"].ToString();
Feb 04, 2013 11:47 PM|LINK
You can find a rather exhaustive list of all of the available properties / attributes for the Active Directory
You may want to try using "displayName" (not sure about case-sensitivity in comparison to your current usage)
string s = result[i].Properties["displayName"].ToString(); //Display-Name property
string s = result[i].Properties["lDAPDisplayName"].ToString(); //LDAP-Display-Name property
Feb 04, 2013 11:50 PM|LINK
They both return:
Feb 05, 2013 12:24 AM|LINK
Have you tried removing the .ToString() after your properties or using the debugger and placing a breakpoint within the loop to see what kind of properties are available on your result?
(You can use the QuickWatch or Locals in Visual Studio to check the value of result[i].Properties["YourProperty"] etc.)
Feb 05, 2013 12:30 PM|LINK
Yes, I have and it did not work.
Feb 05, 2013 12:42 PM|LINK
Did you step through your code within your loop and check out your 'results' variable and see what kinds of properties it has? It can often be a great way to extract exactly what you need in situations like this.
Feb 05, 2013 02:38 PM|LINK
Yes, I have and it returns null.
Feb 05, 2013 04:24 PM|LINK
How do I change my original code to retrieve all users and then filter users instead of hard coding a user name?