Home All Groups Group Topic Archive Search About

Accessing properties in Active Directory

Author
7 Feb 2006 3:26 PM
Derek
I am having problems retrieving property values from Active Directory using
Visual Web Developer 2005 Express Edition.

I want to populate a form on the company intranet from Active directory and
assign roles to the user logged on depending on their department. To start
with I am just trying to display properties.

I have created a class as follows:

Imports Microsoft.VisualBasic
Imports System
Imports System.DirectoryServices
Imports System.Data

Namespace ADAccess

    Public NotInheritable Class ActiveDirectory
        Private _Properties As New StringCollection

        Public ReadOnly Property Props()
            Get
                Return _Properties
            End Get
        End Property

        Public Sub New()
            Dim Path As String = "LDAP://ou=desktops,ou=domain
users,dc=youngsbluecrest,dc=com"
            Dim Entry As New System.DirectoryServices.DirectoryEntry(Path)
            Dim Searcher As New
System.DirectoryServices.DirectorySearcher(Path)
            Dim Results As System.DirectoryServices.SearchResultCollection
            Dim Result As System.DirectoryServices.SearchResult

            Searcher.Filter = ("(objectClass=User)")
            Searcher.PropertiesToLoad.Add("firstname")
            Searcher.PropertiesToLoad.Add("lastname")
            Searcher.PropertiesToLoad.Add("department")
            Searcher.PropertiesToLoad.Add("telephonenumber")
            Searcher.PropertiesToLoad.Add("email")
            Searcher.PropertiesToLoad.Add("displayname")
            Searcher.PropertiesToLoad.Add("description")
            Searcher.PropertiesToLoad.Add("office")
            Searcher.PropertiesToLoad.Add("street")
            Searcher.PropertiesToLoad.Add("pobox")
            Searcher.PropertiesToLoad.Add("city")
            Searcher.PropertiesToLoad.Add("home")
            Searcher.PropertiesToLoad.Add("pager")
            Searcher.PropertiesToLoad.Add("mobile")

            Results = Searcher.FindAll

            For Each Result In Results

_Properties.Add(Result.Properties.Item("department").ToString())
            Next

            Searcher.Dispose()
            Entry.Dispose()
        End Sub

    End Class

End Namespace

I have a form with label on it. The page load runs the following code.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
    Dim AD As New ADAccess.ActiveDirectory()
    Dim Str As String = ""

    For Each Name As String In AD.Props
        Str &= Name + vbNewLine
    Next

    Label1.Text = Str
End Sub

Instead of returning the value of “First Name” I get
“System.DirectoryServices.ResultPropertyValueCollection” for every user in
Active Directory.

--
Derek

Author
7 Feb 2006 8:31 PM
NuTcAsE
The statement Result.Properties.Item("department") returns a
ResultPropertyValueCollection class which contains a collection of
values for that property. To get the underlying value try this:

Dim propertyValues as
System.DirectoryServices.ResultPropertyValueCollection

For Each Result In Results
  propertyValues = Result.Properties.Item("department")
  -- This check is required to make sure that a value is returned for
the specified property.
  If (propertyValues.Count > 0) Then
   _Properties.Add (propertyValues.Item(0).ToString());
  End If
Next


Hope this helps...
- NuTcAsE

AddThis Social Bookmark Button