Solved

How do I list all property names in a VB.net LDAP connection?

Posted on 2011-09-07
4
1,100 Views
Last Modified: 2012-05-12
Hello folks.  What I need to do is query my AD and get a list of all available property names for each user in my AD.   (like First Name, Last Name...etc..etc..) (not that actual values for each property, but the property names themselves) There are some custom tabs when you look at a user acct in my active directory users and computers and I don't know the actual name of them to be able to reference them in my code.  I can easily get the values for things like "givenname" and "mail", however I simply want to retrieve a list of all of the possible property names so I can query any individual one for it's value for a specific user.  
Below is the code i'm using to connect to LDAP.  Can anyone add to or modify the code below allowing me to properly display what i'm needing?

thanks again!

Dim oSearcher As New DirectorySearcher
        Dim oResults As SearchResultCollection
        Dim oResult As SearchResult
        Dim RetArray As New ArrayList
        Dim mCount As Integer
        Dim mIdx As Integer
        Dim mLDAPRecord As String

        Dim ResultFields() As String = {"securityEquals", "cn"}

        Try
            With oSearcher

                .SearchRoot = New DirectoryEntry("LDAP://client.mydomain.net/DC=client,DC=mydomain,DC=net")
                .PropertiesToLoad.AddRange(ResultFields)
                '.Filter = "cn=" & pFindWhat & "*"
                .Filter = "givenname=" & pFindWhat & "*"
                oResults = .FindAll()

            End With

Open in new window


for example, the image below has your common properties shown for a user in AD.  I think those are the standard properties and I have some custom tabs that have more properties similar to below.  How do I get the actual names used to reference them in code so as to retrieve it's value?
 ldap
0
Comment
Question by:linuxrox
[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
4 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 36504023
You need to use Reflection to do this. Here is an example that I've worked from in the past. You just need to update it to suit your needs (i.e., change the object being reflected)

http://www.vb-helper.com/howto_net_system_information.html
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36504107
The DirectoryEntry class has a Properties collection.

Function to Print DirectoryEntry properties
http://geekswithblogs.net/mnf/archive/2006/04/20/75646.aspx
Public Shared Sub PrintDirectoryEntryProperties(entry As System.DirectoryServices.DirectoryEntry, sComment As String)
	' loop through all the properties and get the key for each
	For Each Key As String In entry.Properties.PropertyNames
		Dim sPropertyValues As String = [String].Empty
		' now loop through all the values in the property;
		' can be a multi-value property
		For Each Value As Object In entry.Properties(Key)
			sPropertyValues += Convert.ToString(Value) & ";"
		Next
		' cut off the separator at the end of the value list
		sPropertyValues = sPropertyValues.Substring(0, sPropertyValues.Length - 1)
		' now add the property info to the property list
		Debug.WriteLine(Key & "=" & sPropertyValues)
	Next
End Sub

Open in new window

0
 

Author Comment

by:linuxrox
ID: 36504310
interesting.  I just don't know what the object is called that I am supposed to use.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 36506769
Oh, yes, you are using SearchResult, which has the Properties collection, too:

SearchResult.Properties Property
http://msdn.microsoft.com/en-us/library/system.directoryservices.searchresult.properties.aspx
' Get the properties for 'mySearchResult'.
Dim myResultPropColl As ResultPropertyCollection
myResultPropColl = mySearchResult.Properties
Console.WriteLine("The properties of the 'mySearchResult' are :")
Dim myKey As String
For Each myKey In  myResultPropColl.PropertyNames
   Dim tab1 As String = "    "
   Console.WriteLine(myKey + " = ")
   Dim myCollection As Object
   For Each myCollection In  myResultPropColl(myKey)
      Console.WriteLine(tab1 + myCollection)
   Next myCollection
Next myKey

Open in new window

0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

635 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