Last post Feb 10, 2013 06:17 PM by Rion Williams
Feb 04, 2013 06:56 PM|khurj01|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 07:09 PM|Rion Williams|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 07:28 PM|khurj01|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 07:47 PM|Rion Williams|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 07:50 PM|khurj01|LINK
They both return:
Feb 04, 2013 08:24 PM|Rion Williams|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 08:30 AM|khurj01|LINK
Yes, I have and it did not work.
Feb 05, 2013 08:42 AM|Rion Williams|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 10:38 AM|khurj01|LINK
Yes, I have and it returns null.
Feb 05, 2013 12:24 PM|khurj01|LINK
How do I change my original code to retrieve all users and then filter users instead of hard coding a user name?
Feb 10, 2013 06:17 PM|Rion Williams|LINK
You may want to try using the PrincipleContext to accomplish this, as mentioned in
this question on Stack Overflow. This link might be helpful as well.