Query to Active Directory

G

greg.hart

Hello all,

I just wanted to see if someone can see something I am missing. This
code works, but does not seem to apply the filter. It pulls back all
the users whereas I am just looking for the ones that have name and
telephone populated and are not disabled. When I run this filter
directly inside the AD query tool I get the expected results. Just
doesn't seem to work right from my code. Thanks.

PrincipalContext ctx = new PrincipalContext(ContextType.Domain,
<Domain>,<UserName>,<Password>);
UserPrincipal u = new UserPrincipal(ctx);
u.AdvancedSearchFilter.Equals("(&(objectCategory=person)
(objectClass=user)(name=*)(telephoneNumber=*)(!userAccountControl:
1.2.840.113556.1.4.803:=2))");

PrincipalSearcher ps = new PrincipalSearcher(u);
PrincipalSearchResult<Principal> fr = ps.FindAll();

foreach (UserPrincipal usr in fr)
{

Response.Write(usr.DisplayName + "&nbsp;&nbsp;" +
usr.VoiceTelephoneNumber + "<br />");

}
 
G

Guest

Hello all,

I just wanted to see if someone can see something I am missing.  This
code works, but does not seem to apply the filter.  It pulls back all
the users whereas I am just looking for the ones that have name and
telephone populated and are not disabled.  When I run this filter
directly inside the AD query tool I get the expected results.  Just
doesn't seem to work right from my code.  Thanks.

 PrincipalContext ctx = new PrincipalContext(ContextType.Domain,
<Domain>,<UserName>,<Password>);
            UserPrincipal u = new UserPrincipal(ctx);
            u.AdvancedSearchFilter.Equals("(&(objectCategory=person)
(objectClass=user)(name=*)(telephoneNumber=*)(!userAccountControl:
1.2.840.113556.1.4.803:=2))");

            PrincipalSearcher ps = new PrincipalSearcher(u);
            PrincipalSearchResult<Principal> fr = ps.FindAll();

            foreach (UserPrincipal usr in fr)
            {

                Response.Write(usr.DisplayName + "&nbsp;&nbsp;" +
usr.VoiceTelephoneNumber + "<br />");

            }

You can't use AdvancedSearchFilter in that way. First of all, Equals
function does nothing, but returns a boolean result. Then the
PrincipalSearcher class required to set its properties to create a
searcher and you can't use LDAP queries for setting up search filters
(objectClass=user), etc to do this.

Instead of line with u.AdvancedSearchFilter.Equals....

use:

u.Enabled = false;
u.Name = "*";

and you should get the correct result.

Read more about PrincipalSearcher

http://msdn.microsoft.com/en-us/lib...ices.accountmanagement.principalsearcher.aspx
http://msdn.microsoft.com/en-us/magazine/cc135979.aspx (Finding
Matches)

Hope this helps
 
G

greg.hart

You can't use AdvancedSearchFilter in that way. First of all, Equals
function does nothing, but returns a boolean result. Then the
PrincipalSearcher class required to set its properties to create a
searcher and you can't use LDAP queries for setting up search filters
(objectClass=user), etc  to do this.

Instead of line with u.AdvancedSearchFilter.Equals....

use:

u.Enabled = false;
u.Name = "*";

and you should get the correct result.

Read more about PrincipalSearcher

http://msdn.microsoft.com/en-us/lib...soft.com/en-us/magazine/cc135979.aspx(Finding
Matches)

Hope this helps- Hide quoted text -

- Show quoted text -

Thank you Alexy!

I was testing this inside a SharePoint aspx page and did have the
Intellisense for the UserPrincipal. Here is the code I used, maybe it
can help someone. It returns the users from AD that are enabled, have
a populated first name, and have a phone number.


PrincipalContext ctx = new PrincipalContext(ContextType.Domain,
<Domain>,<UserName>,<Password>);
UserPrincipal u = new UserPrincipal(ctx);
u.Enabled = true;
u.Name = "*";
u.VoiceTelephoneNumber = "*";

PrincipalSearcher ps = new PrincipalSearcher(u);
PrincipalSearchResult<Principal> fr = ps.FindAll();

foreach (UserPrincipal usr in fr)
{

Response.Write(usr.DisplayName + "&nbsp;&nbsp;" +
usr.VoiceTelephoneNumber + "<br />");

}
 
G

Guest

Thank you Alexy!

I was testing this inside a SharePoint aspx page and did have the
Intellisense for the UserPrincipal.  Here is the code I used, maybe it
can help someone.  It returns the users from AD that are enabled, have
a populated first name, and have a phone number.

   PrincipalContext ctx = new PrincipalContext(ContextType.Domain,
<Domain>,<UserName>,<Password>);
            UserPrincipal u = new UserPrincipal(ctx);
            u.Enabled = true;
            u.Name = "*";
            u.VoiceTelephoneNumber = "*";

            PrincipalSearcher ps = new PrincipalSearcher(u);
            PrincipalSearchResult<Principal> fr = ps.FindAll();

            foreach (UserPrincipal usr in fr)
            {

                Response.Write(usr.DisplayName + "&nbsp;&nbsp;" +
usr.VoiceTelephoneNumber + "<br />");

            }

Glad, it works for you
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top