I have spent a lot of time googling the web, looking for information on how to populate a dropdown list box with employee names from Active Directory.
I am coming up short.
What I really help on from code below is how to populate name into the emplist dropdownlist control id.
Please help.
Thanks much in advance.
Private Sub FillDropdown()
Dim oroot As DirectoryEntry = New DirectoryEntry("LDAP://domain.com/OU=Departments,DC=domain,DC=com")
Dim osearcher As DirectorySearcher = New DirectorySearcher(oroot)
Dim oresult As SearchResultCollection
Dim result As SearchResult
Dim depts As New SortedList()
osearcher.Filter = "(&(objectCategory=person))" ' search filter
osearcher.PropertiesToLoad.Add("cn") ' username
osearcher.PropertiesToLoad.Add("name") ' full name
osearcher.PropertiesToLoad.Add("givenname") ' firstname
osearcher.PropertiesToLoad.Add("sn") ' lastname
osearcher.PropertiesToLoad.Add("mail") ' mail
osearcher.PropertiesToLoad.Add("initials") ' initials
osearcher.PropertiesToLoad.Add("ou") ' organizational unit
osearcher.PropertiesToLoad.Add("userPrincipalName") ' login name
osearcher.PropertiesToLoad.Add("distinguishedName") ' distinguised name
oresult = osearcher.FindAll()
For Each result In oresult
If Not result.GetDirectoryEntry.Properties("sn").Value Is Nothing Then
' writes specific values retrieved from above - this is just a sample.
Response.Write(result.GetDirectoryEntry.Properties("cn").Value & ":" & result.GetDirectoryEntry.Properties("name").Value)
End If
Next
emplist.DataSource = depts
emplist.DataTextField = "Value"
emplist.DataValueField = "Key"
emplist.DataBind()
End Sub
ForEach result
In oresult IfNot result.GetDirectoryEntry.Properties("sn").ValueIsNothingThen ' writes specific values retrieved from above - this is just a sample.
Response.Write(result.GetDirectoryEntry.Properties("cn").Value & ":" & result.GetDirectoryEntry.Properties("name").Value)
End If
It returns results.
I am just trying to add those results into dropdownlist. I am using asp.net dropdownlist.
So, getting those values into dropdown is what I am trying to accomplish.
You dont really need a sortedList to save the collection, just save it to a generic List<String> instead since you only wanted to save the names. This should work
Dim oroot AsDirectoryEntry = NewDirectoryEntry("LDAP://domain.com/OU=Departments,DC=domain,DC=com")
Dim osearcher AsDirectorySearcher = NewDirectorySearcher(oroot)
Dim oresult AsSearchResultCollectionDim result AsSearchResultDim list AsNewList(OfString)
osearcher.Filter = "(&(objectCategory=person))"' search filter
osearcher.PropertiesToLoad.Add("cn") ' username
osearcher.PropertiesToLoad.Add("name") ' full name
osearcher.PropertiesToLoad.Add("givenname") ' firstname
osearcher.PropertiesToLoad.Add("sn") ' lastname
osearcher.PropertiesToLoad.Add("mail") ' mail
osearcher.PropertiesToLoad.Add("initials") ' initials
osearcher.PropertiesToLoad.Add("ou") ' organizational unit
osearcher.PropertiesToLoad.Add("userPrincipalName") ' login name
osearcher.PropertiesToLoad.Add("distinguishedName") ' distinguised name
oresult = osearcher.FindAll()
ForEach result In oresult
IfNot result.GetDirectoryEntry.Properties("sn").Value IsNothingThen
list.Add(result.GetDirectoryEntry.Properties("sn").Value.ToString())
EndIfNextemplist.DataSource = list;
emplist.DataBind()
Marked as answer by simflex on Jun 25, 2012 07:05 PM
simflex
Member
80 Points
281 Posts
How do I populate Dropdownlist with users from Active Directory?
Jun 25, 2012 03:05 PM|LINK
Greetings experts,
I have spent a lot of time googling the web, looking for information on how to populate a dropdown list box with employee names from Active Directory.
I am coming up short.
What I really help on from code below is how to populate name into the emplist dropdownlist control id.
Please help.
Thanks much in advance.
Private Sub FillDropdown() Dim oroot As DirectoryEntry = New DirectoryEntry("LDAP://domain.com/OU=Departments,DC=domain,DC=com") Dim osearcher As DirectorySearcher = New DirectorySearcher(oroot) Dim oresult As SearchResultCollection Dim result As SearchResult Dim depts As New SortedList() osearcher.Filter = "(&(objectCategory=person))" ' search filter osearcher.PropertiesToLoad.Add("cn") ' username osearcher.PropertiesToLoad.Add("name") ' full name osearcher.PropertiesToLoad.Add("givenname") ' firstname osearcher.PropertiesToLoad.Add("sn") ' lastname osearcher.PropertiesToLoad.Add("mail") ' mail osearcher.PropertiesToLoad.Add("initials") ' initials osearcher.PropertiesToLoad.Add("ou") ' organizational unit osearcher.PropertiesToLoad.Add("userPrincipalName") ' login name osearcher.PropertiesToLoad.Add("distinguishedName") ' distinguised name oresult = osearcher.FindAll() For Each result In oresult If Not result.GetDirectoryEntry.Properties("sn").Value Is Nothing Then ' writes specific values retrieved from above - this is just a sample. Response.Write(result.GetDirectoryEntry.Properties("cn").Value & ":" & result.GetDirectoryEntry.Properties("name").Value) End If Next emplist.DataSource = depts emplist.DataTextField = "Value" emplist.DataValueField = "Key" emplist.DataBind() End SubamosCabanban...
Member
441 Points
148 Posts
Re: How do I populate Dropdownlist with users from Active Directory?
Jun 25, 2012 05:10 PM|LINK
Compare your logic sequence to my working sample below
var ADService = new ActiveDirectoryServiceTest();
DirectorySearcher mySearcher;
var entry = ADService.GetDirectoryEntry(LDAPPath, UserName, Password, CompanyName);
entry.AuthenticationType = AuthenticationTypes.Secure;
var listOfUser = ActiveDirectoryServiceTest.InitializeSearcher(ActiveDirectoryFiltersTest.GetFilterByDisplayName(DisplayName), entry, out mySearcher);
mySearcher.SearchScope = SearchScope.Subtree;
mySearcher.CacheResults = false;
SearchResultCollection resultUsers = mySearcher.FindAll();
SortedDictionary<String, String> list = new SortedDictionary<String, String>();
var user = resultUsers[0];
if (user != null)
{
foreach (string propName in user.Properties.PropertyNames)
{
ResultPropertyValueCollection valueCollection = user.Properties[propName];
foreach (Object propertyValue in valueCollection)
{
if (!list.Keys.Contains(propName))
list.Add(propName, propertyValue.ToString());
}
}
}
mySearcher.Dispose();
entry.Dispose();
GridView1.DataSource = list;
GridView1.DataBind();
amosCabanban...
Member
441 Points
148 Posts
Re: How do I populate Dropdownlist with users from Active Directory?
Jun 25, 2012 05:12 PM|LINK
simflex
Member
80 Points
281 Posts
Re: How do I populate Dropdownlist with users from Active Directory?
Jun 25, 2012 05:28 PM|LINK
Hi,
Thanks for your help. It would help me understand what this code is doing though. Is it populating a dropdownlist with users names?
Also, a bit of a silly question - Is your code written in c# or c++?
I am trying to convert it to vb but it keeps bumming.
amosCabanban...
Member
441 Points
148 Posts
Re: How do I populate Dropdownlist with users from Active Directory?
Jun 25, 2012 05:35 PM|LINK
can you verify first if your searcher is returning any results?
simflex
Member
80 Points
281 Posts
Re: How do I populate Dropdownlist with users from Active Directory?
Jun 25, 2012 06:01 PM|LINK
Yes, if response.write it as in the code below:
For Each result In oresult
If Not result.GetDirectoryEntry.Properties("sn").Value Is Nothing Then
' writes specific values retrieved from above - this is just a sample.
Response.Write(result.GetDirectoryEntry.Properties("cn").Value & ":" & result.GetDirectoryEntry.Properties("name").Value)
End If
It returns results.
I am just trying to add those results into dropdownlist. I am using asp.net dropdownlist.
So, getting those values into dropdown is what I am trying to accomplish.
simflex
Member
80 Points
281 Posts
Re: How do I populate Dropdownlist with users from Active Directory?
Jun 25, 2012 06:01 PM|LINK
sorry this is dup; so removed it
amosCabanban...
Member
441 Points
148 Posts
Re: How do I populate Dropdownlist with users from Active Directory?
Jun 25, 2012 06:09 PM|LINK
simflex
Member
80 Points
281 Posts
Re: How do I populate Dropdownlist with users from Active Directory?
Jun 25, 2012 06:31 PM|LINK
Thank you very much. The dropdownlist is getting populated YEA!
Do you know how to sort it?
I would like the names sorted.
amosCabanban...
Member
441 Points
148 Posts
Re: How do I populate Dropdownlist with users from Active Directory?
Jun 25, 2012 06:43 PM|LINK
call list.Sort()