?
Solved

Active Directory Search not working

Posted on 2014-03-13
3
Medium Priority
?
296 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 2000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
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 …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Suggested Courses

801 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