• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

Active Directory Search not working

Using VB.NET

Trying to retreive the 'givenname' and 'SN' from Active Directory using the following code:

<snip>
oSearcher.Filter = "(&(objectCategory=user)(objectClass=user)(samaccountname=" & pUsername & "))"

        oSearcher.PropertiesToLoad.AddRange(New String() {"givenName", "SN"}
        Try
            oResults = oSearcher.FindAll

            For Each oResult In oResults
                returnVal = oResult.Properties("givenname").ToString()
            Next

Open in new window


but 'returnVal' is set to: "System.DirectoryServices.ResultPropertyValueCollection" and not the givenname

The 'DirectoryEntry' for my DC etc. is correct as I'm using the same thing for another query that works just fine and is very similar to this one.

I get no actual error, just not gettng the data I need

What am I missing?
0
BrianFord
Asked:
BrianFord
  • 2
1 Solution
 
Robert SchuttSoftware EngineerCommented:
Well I can't seem to find a useful example in the documentation but this works for me:
returnVal = oResult.Properties("givenname")(0).ToString

Open in new window

So add the (0) because the property value is actually a collection of values.

In some cases you need to be aware that a property may have no value or more than 1 value.
0
 
Robert SchuttSoftware EngineerCommented:
Ah, now also found a more generic example here, should that be applicable (wanting to show all properties found):
        Dim propKey As String
        For Each propKey In oResult.Properties.PropertyNames
            ' Display each of the values for the property 
            ' identified by the property name.
            Dim prop As Object
            For Each prop In oResult.Properties(propKey)
                Console.WriteLine("{0}:{1}", propKey, [prop].ToString())
            Next prop
        Next propKey

Open in new window

0
 
BrianFordAuthor Commented:
Adding the zero did the trick :)
Thanks very much for your help
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now