Solved

Active Directory Search not working

Posted on 2014-03-13
3
287 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
  • 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

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now