// 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.
Member
33 Points
350 Posts
User List From Active Directory
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?
All-Star
114593 Points
18503 Posts
MVP
Re: User List From Active Directory
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 :
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.
Member
33 Points
350 Posts
Re: User List From Active Directory
Feb 04, 2013 07:28 PM|khurj01|LINK
The string s won't return any values
All-Star
114593 Points
18503 Posts
MVP
Re: User List From Active Directory
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 here.
You may want to try using "displayName" (not sure about case-sensitivity in comparison to your current usage) or "lDAPDisplayName"
Member
33 Points
350 Posts
Re: User List From Active Directory
Feb 04, 2013 07:50 PM|khurj01|LINK
They both return:
System.DirectoryServices.ResultPropertyValueCollection
All-Star
114593 Points
18503 Posts
MVP
Re: User List From Active Directory
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.)
Member
33 Points
350 Posts
Re: User List From Active Directory
Feb 05, 2013 08:30 AM|khurj01|LINK
Yes, I have and it did not work.
All-Star
114593 Points
18503 Posts
MVP
Re: User List From Active Directory
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.
Member
33 Points
350 Posts
Re: User List From Active Directory
Feb 05, 2013 10:38 AM|khurj01|LINK
Yes, I have and it returns null.
Member
33 Points
350 Posts
Re: User List From Active Directory
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?
All-Star
114593 Points
18503 Posts
MVP
Re: User List From Active Directory
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.