|
-
Oct 17th, 2007, 08:56 AM
#1
Thread Starter
Member
[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
-
Oct 17th, 2007, 09:42 AM
#2
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())
-
Oct 17th, 2007, 09:53 AM
#3
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.
-
Oct 17th, 2007, 10:05 AM
#4
Thread Starter
Member
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?
-
Oct 17th, 2007, 10:09 AM
#5
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.
-
Oct 17th, 2007, 10:15 AM
#6
Thread Starter
Member
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?
-
Oct 17th, 2007, 10:39 AM
#7
Re: grab all users from active directory (directoryservices?)
 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 .
-
Oct 17th, 2007, 11:13 AM
#8
Re: grab all users from active directory (directoryservices?)
 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?
-
Oct 17th, 2007, 11:32 AM
#9
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|