Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 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