Solved

Active Directory Search not working

Posted on 2014-03-13
3
295 Views
Last Modified: 2014-03-14
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
Comment
Question by:BrianFord
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39928740
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
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39928756
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
 

Author Closing Comment

by:BrianFord
ID: 39928892
Adding the zero did the trick :)
Thanks very much for your help
0

Featured Post

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In-place Upgrading Dirsync to Azure AD Connect
Here's a look at newsworthy articles and community happenings during the last month.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question