Results 1 to 9 of 9

Thread: [RESOLVED] grab all users from active directory (directoryservices?)

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    56

    Resolved [RESOLVED] grab all users from active directory (directoryservices?)

    I'm creating a web application for in house using vb.net 2005. I have a list box that I want to populate with all the people in the company. I know that everyone is listed in active directory, so i was wondering if i could somehow grab all those names (possibly email addresses?) from active directory with my program and put them in the list box. Ideally I'd want to show the name and have the value be the email address. But the name is the important thing. Do I have to use directory services? I'm assuming so, but I have no clue how to do it. Can anyone show me how to do something like this? Thank you much! Let me know if more info is needed.
    Amanda

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: grab all users from active directory (directoryservices?)

    You need to add a reference to System.directoryservices to your app and then import System.DirectoryServices.

    You will have to modify the domain string to match your domain:
    Code:
            Dim domain As DirectoryEntry = New DirectoryEntry("LDAP://yourdomain.com/CN=Users,DC=yourdomain,DC=com")
    
    
            Dim sb As New System.Text.StringBuilder()
    
            For Each User As DirectoryEntry In domain.Children
    
                Console.WriteLine(User.Name)
                sb.AppendLine(User.Properties("DisplayName").Value + " - " + User.Properties("mail").Value)
            Next
    
            MessageBox.Show(sb.ToString())

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: grab all users from active directory (directoryservices?)

    This looked pretty darn cool so I thought I would try it out.

    This line
    Code:
    sb.AppendLine(User.Properties("DisplayName").Value + " - " + User.Properties("mail").Value)
    gives me:

    Option Strict On prohibits operands of type Object for operator '+'

    Changing the + sign to the & sign gave me the same thing for that operator.

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    56

    Re: grab all users from active directory (directoryservices?)

    When I do that, I'm getting things like:

    System
    BuiltIn
    Computers
    Domain Controllers
    Infrastructure
    Users
    etc.

    And then the mail and DisplayName is always nothing. So all I'm ending up with is a bunch of dashes. Any suggestions?

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: grab all users from active directory (directoryservices?)

    Big Red: Do you have Option Strict On?

    If not, then that is why you got as far as you did. If you put it on, you should get the same error that I got.

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    56

    Re: grab all users from active directory (directoryservices?)

    I'm not sure about Option Strict. I don't know how to set it. Is this actually a property of a web application or just projects/windows apps?

  7. #7
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: grab all users from active directory (directoryservices?)

    Quote Originally Posted by Hack
    This looked pretty darn cool so I thought I would try it out.

    This line
    Code:
    sb.AppendLine(User.Properties("DisplayName").Value + " - " + User.Properties("mail").Value)
    gives me:

    Option Strict On prohibits operands of type Object for operator '+'

    Changing the + sign to the & sign gave me the same thing for that operator.
    Sorry about that, I program more in C# these days .

  8. #8
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: grab all users from active directory (directoryservices?)

    Quote Originally Posted by Big Red
    When I do that, I'm getting things like:

    System
    BuiltIn
    Computers
    Domain Controllers
    Infrastructure
    Users
    etc.

    And then the mail and DisplayName is always nothing. So all I'm ending up with is a bunch of dashes. Any suggestions?
    Did you include the "/CN=Users" in your LDAP string?

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    56

    Resolved Re: grab all users from active directory (directoryservices?)

    It should've been in there, but here is actually my code that works. I found a way to do it:

    Code:
    cmbworkers.Items.Clear()
    
            Dim objsearch As DirectorySearcher = New DirectorySearcher()
            Dim strrootdse As String = objsearch.SearchRoot.Path
          
            Dim allusers As String
            Dim searchroot As New DirectoryEntry(strrootdse)
            Dim search As New DirectorySearcher(searchroot)
    
            'search.Filter = "(&(objectClass=user)(objectCategory=person)(showInAddressBook=*))" '---This is used if not using the if statement below checking if it's person
            search.Filter = "(&(objectClass=user)(showInAddressBook=*))"
            search.PropertiesToLoad.Add("mail")
            search.Sort.Direction = SortDirection.Ascending
            search.Sort.PropertyName = "mail"
            search.PropertiesToLoad.Add("DisplayName")
            search.PropertiesToLoad.Add("objectClass")
    
            Dim result As SearchResult
            Dim resultcol As SearchResultCollection = search.FindAll()
    
                   For Each result In resultcol
                Try
    
                    Dim newitem As New Web.UI.WebControls.ListItem
                    If result.GetDirectoryEntry().Properties("objectClass").Item(1) = "person" Then
                        newitem.Text = result.GetDirectoryEntry().Properties("DisplayName").Value
                    Else
                        newitem.Text = "[" & result.GetDirectoryEntry().Properties("DisplayName").Value & "]"
                    End If
                    newitem.Value = result.GetDirectoryEntry().Properties("mail").Value
                    newitem.Text = result.GetDirectoryEntry().Properties("DisplayName").Value
                    'newitem.Attributes.CssStyle.Item("font-size") = "20pt"
                    cmbworkers.Items.Add(newitem)
    
                                Catch ex As Exception
    
                End Try
            Next
    Although, I'm pretty sure your code worked too. Thanks for the help. I really appreciate it!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width