Solved

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

Posted on 2011-09-07
4
865 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
  • 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

685 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