Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-07
4
Medium Priority
?
1,399 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 2000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

824 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