I am using DirectorySearcher class to query the active directory. It gives all the records in a single page (more than 5000). I want to get 100 records per page. So I moved to SearchRequest class. Using SearchRequest class I can get 100 records per page.
But for particular query it is not working. I want to get all the users with their
"samaccountname or displayname starts with 'a'" works fine. Then I want to get all the users with their
"samaccountname and displayname starts with 'a'", this it is not working. I can guess the reason, some of the users starts their samaccountname with a not having any displayname. Any workaround for this issue?
Please refer the following code,
//This query works fine
//string filter = "(&(objectCategory=person)(objectClass=user)(!sAMAccountType=805306370)(|(samaccountname=a*)(displayname=a*)))";
/* Not works */
string filter = "(&(objectCategory=person)(objectClass=user)(!sAMAccountType=805306370)(&(samaccountname=a*)(displayname=a*)))";
LdapConnection connection = new LdapConnection(serverName);
string[] attribs = { "samaccountname", "displayname" };
// create a SearchRequest object
SearchRequest searchRequest = new SearchRequest
(scope,
filter,
System.DirectoryServices.Protocols.SearchScope.Subtree,
attribs);
SortRequestControl sortRequest = new SortRequestControl("samaccountname", false);
searchRequest.Controls.Add(sortRequest);
VlvRequestControl vlvRequest =
new VlvRequestControl(0, numEntries, offsetVal);
searchRequest.Controls.Add(vlvRequest);
SearchResponse searchResponse =
(SearchResponse)connection.SendRequest(searchRequest);
if (searchResponse.Controls.Length != 2 ||
!(searchResponse.Controls[0] is SortResponseControl))
{
Console.WriteLine("The server does not support VLV");
return null;
}
Thanks for your reply Dino. Your query (&(samaccountname=a*)(&(displayname=a*)(!displayname=\00))) returns search filter is not valid error message. If I change the query like (&(samaccountname=a*)(&(displayname=a*)(!displayname=\\00))) , it falls in server
does not support vlv case. Any suggestion?
Tamilmani Kp...
Participant
1091 Points
265 Posts
System.DirectoryServices.Protocols.SearchRequest Ldap Query Execution Problem
Feb 09, 2012 07:04 AM|LINK
I am using DirectorySearcher class to query the active directory. It gives all the records in a single page (more than 5000). I want to get 100 records per page. So I moved to SearchRequest class. Using SearchRequest class I can get 100 records per page. But for particular query it is not working. I want to get all the users with their "samaccountname or displayname starts with 'a'" works fine. Then I want to get all the users with their "samaccountname and displayname starts with 'a'", this it is not working. I can guess the reason, some of the users starts their samaccountname with a not having any displayname. Any workaround for this issue?
Please refer the following code,
//This query works fine //string filter = "(&(objectCategory=person)(objectClass=user)(!sAMAccountType=805306370)(|(samaccountname=a*)(displayname=a*)))"; /* Not works */ string filter = "(&(objectCategory=person)(objectClass=user)(!sAMAccountType=805306370)(&(samaccountname=a*)(displayname=a*)))"; LdapConnection connection = new LdapConnection(serverName); string[] attribs = { "samaccountname", "displayname" }; // create a SearchRequest object SearchRequest searchRequest = new SearchRequest (scope, filter, System.DirectoryServices.Protocols.SearchScope.Subtree, attribs); SortRequestControl sortRequest = new SortRequestControl("samaccountname", false); searchRequest.Controls.Add(sortRequest); VlvRequestControl vlvRequest = new VlvRequestControl(0, numEntries, offsetVal); searchRequest.Controls.Add(vlvRequest); SearchResponse searchResponse = (SearchResponse)connection.SendRequest(searchRequest); if (searchResponse.Controls.Length != 2 || !(searchResponse.Controls[0] is SortResponseControl)) { Console.WriteLine("The server does not support VLV"); return null; }Efforts Never Fail...
Dino He - MS...
Star
8068 Points
1023 Posts
Microsoft
Re: System.DirectoryServices.Protocols.SearchRequest Ldap Query Execution Problem
Feb 13, 2012 01:02 AM|LINK
Hi
Please try this:
Use Null value in LDAP.
Hope it helpful.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
Tamilmani Kp...
Participant
1091 Points
265 Posts
Re: System.DirectoryServices.Protocols.SearchRequest Ldap Query Execution Problem
Feb 13, 2012 03:59 AM|LINK
Thanks for your reply Dino. Your query (&(samaccountname=a*)(&(displayname=a*)(!displayname=\00))) returns search filter is not valid error message. If I change the query like (&(samaccountname=a*)(&(displayname=a*)(!displayname=\\00))) , it falls in server does not support vlv case. Any suggestion?
Efforts Never Fail...
Dino He - MS...
Star
8068 Points
1023 Posts
Microsoft
Re: System.DirectoryServices.Protocols.SearchRequest Ldap Query Execution Problem
Feb 16, 2012 12:28 AM|LINK
Hi
I think your problem is because you can't deal with the null value.
And more detail:
http://forums.asp.net/t/1738986.aspx/1
http://blogs.dirteam.com/blogs/tomek/archive/2005/11/21/_5B00_R_5D00_-Using-LDAP-search-filter-to-query-attributes-without-value.aspx
(!displayname=*) this should be null value.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework