Last post Dec 22, 2010 08:48 AM by smirnov
Oct 26, 2010 10:24 AM|luv_kermie|LINK
I have created a program that will search our companies AD and bring back the results. I have been successful in searching via samaccountname and givenName, but when I attempt a lastName search it doesn't bring anything back.
oSearcher.Filter = ("(&(objectClass=user)(givenName=" & firstName & "))")
oSearcher.Filter = ("(&(objectClass=user)(samaccountname=" & logonuser & "))")
But this one does NOT:
oSearcher.Filter = ("(&(objectClass=user)(sn=" & lastName & "))")
Thanks in advance!
Oct 26, 2010 12:26 PM|sansan|LINK
SN can be a Surname also. Try doing a wild char search on that column or seach with display name
"(&(objectClass=user)(objectcategory=person)(displayName=" + "*" + your filter value + "*))"
"(&(objectClass=user)(objectcategory=person)(sn=" + "*" + your filter value + "*))";
Oct 26, 2010 02:06 PM|luv_kermie|LINK
It's not a case sensitive issue. I tested that out, and my givenName search is not.
I attempted the surname property as well with no success. When I find the user using one of the other searches, I am gathering the property using the "sn" convention and it is given me results, so I'm confident using sn.
I tried displayName as well, with no success.
I tried wild cards on all the options you suggested, and yet still no success.
Would it be a rights issue on AD? I don't see why, because if I can see and search by first name and see the last name, I don't see why couldn't search by last name.
Oct 27, 2010 08:51 AM|renrutxela|LINK
i see nothing wrong with the code, i have something very similar which seems to work fine.
mySearcher.Filter = "(&(objectClass=User)(givenName=" & Forename & ")(sn=" & Surname & "))"
so all i can think of is that maybe the "sn" field in AD is not populated. You could try using adsiedit.msc from the windows 2k3 res kit to check the value and the fieldname are as you expect them to be.
microsofts reskit link
Nov 11, 2010 06:17 AM|luv_kermie|LINK
The SN most definitely exists. I can bring up the Active Directory tools and see it. When I search via "givenName", I pull back the "SN" and then using that EXACT same value I query via "SN" and get nothing back. I know the SN exists; I know since I am
programatically pulling it back, that the name is spelled correctly.
Something else is the issue.
Dec 22, 2010 08:48 AM|smirnov|LINK
Two things to check. I know you said, you paste the same sn, but can it be that sn has leading or trailing spaces? This can be easily checked using wildcard search with *, e.g.
Dim lastName As String = "*Smith*"
Dim de As DirectoryEntry = New DirectoryEntry("LDAP://...")
Dim oSearcher As DirectorySearcher = New DirectorySearcher(de)
oSearcher.SearchScope = SearchScope.Subtree
oSearcher.Filter = "(&(objectClass=user)(sn=" & lastName & "))"
Dim users As SearchResultCollection = oSearcher.FindAll()
For Each user As SearchResult In users
& " " & user.Properties("givenName")(0).ToString() _
& " " & user.Properties("sn")(0).ToString())
I noticed a strange thing when using DirectorySearcher. I have two users with sn "Jones_" and "_Davis" (where "_" is for blank space). Using the code above I'm able to find "Jones_" using
I'm confused on how this works...
So, my second suggestion will be to use LDAP Browser and see if search with given filter will work (Tools - Directory Search - Filter).