// 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.
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.)
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.
khurj01
Member
16 Points
241 Posts
User List From Active Directory
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(); for(int i=0;i<userlist.Count;i++) { userlist[i].Properties["displayName"].ToString(); }Rion William...
All-Star
27364 Points
4542 Posts
Re: User List From Active Directory
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.
khurj01
Member
16 Points
241 Posts
Re: User List From 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(); }Rion William...
All-Star
27364 Points
4542 Posts
Re: User List From Active Directory
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 here.
You may want to try using "displayName" (not sure about case-sensitivity in comparison to your current usage) or "lDAPDisplayName"
khurj01
Member
16 Points
241 Posts
Re: User List From Active Directory
Feb 04, 2013 11:50 PM|LINK
They both return:
System.DirectoryServices.ResultPropertyValueCollection
Rion William...
All-Star
27364 Points
4542 Posts
Re: User List From Active Directory
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.)
khurj01
Member
16 Points
241 Posts
Re: User List From Active Directory
Feb 05, 2013 12:30 PM|LINK
Yes, I have and it did not work.
Rion William...
All-Star
27364 Points
4542 Posts
Re: User List From Active Directory
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.
khurj01
Member
16 Points
241 Posts
Re: User List From Active Directory
Feb 05, 2013 02:38 PM|LINK
Yes, I have and it returns null.
khurj01
Member
16 Points
241 Posts
Re: User List From Active Directory
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?
DirectorySearcher searcher = new System.DirectoryServices.DirectorySearcher("(samaccountname=johndoe)"); System.DirectoryServices.SearchResultCollection userlist= searcher.FindAll(); for(int i=0;i<userlist.Count;i++) { userlist[i].Properties["displayName"].ToString(); }