DirectorySearcher - Clauses and keywords for search [Resolved]

Posted by Shoab.Shah under C# on 8/15/2016 | Points: 10 | Views : 123 | Status : [Member] | Replies : 3
Hi All,

I am very new to working Active directory. After spending some time on web i was able to get code for getting users from active directory using DirectorySearcher class. However i am facing hard time in getting a list of all supported clauses and keywords of search.

The only link i was able to get is But got only few keywords.

Kindly share links / list of keywords and clauses. Below are some i found;


Also i tried creating a org chart using Visio and then with DirectorySearcher code. The visio files excludes virtual accounts such as feedback@abc,.com, meeting rooms, etc. but my Directory searcher code get them as welFrown . Kindly help.


Posted by: Rajnilari2015 on: 8/15/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50


Try using PrincipalSearcher( available from .NET 3.5 onwards)

An example

public static Task<IEnumerable<TestUserModelClass>> GetUsers(//Apply your filters)
return Task.Run(() =>
PrincipalContext context = new PrincipalContext(ContextType.Domain);
UserPrincipal principal = new UserPrincipal(context);
principal.Enabled = true;
PrincipalSearcher searcher = new PrincipalSearcher(principal);

var users = searcher.FindAll().Cast<UserPrincipal>()
.Where(x => x.SomeProperty... // Perform queries)
.Select(x => new SomeUserModelClass
userName = x.SamAccountName,
email = x.UserPrincipalName,
guid = x.Guid.Value
}).OrderBy(x => x.userName).AsEnumerable();

return users;

Hope that helps

Thanks & Regards,
RNA Team

Shoab.Shah, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shoab.Shah on: 8/16/2016 [Member] Starter | Points: 25

Thank you Raj!

Shoab.Shah, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhuvanesh6 on: 8/18/2016 [Member] Starter | Points: 25

Two things:

There are no groups associated with an OU - an OU is a container which contains users, computers, groups etc. (like a directory that contains files). Is that what you mean? You want to enumerate the group contained inside a given OU??
if so: you're not calling the constructor for the PrincipalContext properly. If you check the MSDN documentation on PrincipalContext constructors, you'll see that the one you're using is the one with a ContextType and a name which stands for the domain name of the context you want to bind to:

var ctx = new PrincipalContext(ContextType.Domain,"MyOU");

This binds to the MyOU domain - and it binds right at the root of that domain tree.
What you're probably looking for is the constructor with three parameters - a ContextType and two strings - the first one being the domain name as above, and the second one being the start location of your search. So change your PrincipalContext construction to:

var ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", "OU=MyOU");

and then search again - now you should get only groups that are contained inside the OU=MyOU container.



Shoab.Shah, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response