Populating drop down list from AD

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

string strLdap = "LDAP://OU=Sales,DC=Company,DC=com"
DirectoryEntry objOU = new DirectoryEntry(strLdap);
DirectorySearcher objUserSearcher = new DirectorySearcher(objOU);
SearchResultCollection objResults;
//Search for user objects
objUserSearcher.PropertiesToLoad.Add("cn");
objUserSearcher.Filter = "(objectClass=user)";
objResults = objUserSearcher.FindAll();
DropDownList1.Items.Add("New User");
//Loop through the search results adding each one to the combo box
foreach (SearchResult objResult in objResults)
DropDownList1.Items.Add(objResult.Properties["cn"][0]);

What am I doing wrong here...
 
This works for me:

string strLdap =
"LDAP://serverName/CN=Users,OU=Sales,DC=Company,DC=com";
DirectoryEntry objOU = new DirectoryEntry(strLdap, "userName",
"password");
DirectorySearcher objUserSearcher = new DirectorySearcher(objOU,
"(&(objectClass=user)(objCategory=person))");
SearchResultCollection objResults;
//Search for user objects
objUserSearcher.PropertiesToLoad.Add("cn");
objResults = objUserSearcher.FindAll();
DropDownList1.Items.Add("New User");
//Loop through the search results adding each one to the combo box
foreach (SearchResult objResult in objResults)
DropDownList1.Items.Add(objResult.Properties["cn"][0]);
 
Oops, to hasty in copying and pasting; replace this line

DirectorySearcher objUserSearcher = new DirectorySearcher(objOU,
"(&(objectClass=user)(objCategory=person))");

with


DirectorySearcher objUserSearcher = new DirectorySearcher(objOU,
"(objectClass=user)");

or simply use
DirectorySearcher objUserSearcher = new DirectorySearcher(objOU);
and add the Filter you were using in your own code snippet.


--
Carsten Thomsen
Enterprise Development with VS .NET, UML, AND MSF
http://www.apress.com/book/bookDisplay.html?bID=105
Communities - http://community.integratedsolutions.dk

CT said:
This works for me:

string strLdap =
"LDAP://serverName/CN=Users,OU=Sales,DC=Company,DC=com";
DirectoryEntry objOU = new DirectoryEntry(strLdap, "userName",
"password");
DirectorySearcher objUserSearcher = new DirectorySearcher(objOU,
"(&(objectClass=user)(objCategory=person))");
SearchResultCollection objResults;
//Search for user objects
objUserSearcher.PropertiesToLoad.Add("cn");
objResults = objUserSearcher.FindAll();
DropDownList1.Items.Add("New User");
//Loop through the search results adding each one to the combo box
foreach (SearchResult objResult in objResults)
DropDownList1.Items.Add(objResult.Properties["cn"][0]);


--
Carsten Thomsen
Enterprise Development with VS .NET, UML, AND MSF
http://www.apress.com/book/bookDisplay.html?bID=105
Communities - http://community.integratedsolutions.dk

Chris Geier said:
string strLdap = "LDAP://OU=Sales,DC=Company,DC=com"
DirectoryEntry objOU = new DirectoryEntry(strLdap);
DirectorySearcher objUserSearcher = new DirectorySearcher(objOU);
SearchResultCollection objResults;
//Search for user objects
objUserSearcher.PropertiesToLoad.Add("cn");
objUserSearcher.Filter = "(objectClass=user)";
objResults = objUserSearcher.FindAll();
DropDownList1.Items.Add("New User");
//Loop through the search results adding each one to the combo box
foreach (SearchResult objResult in objResults)
DropDownList1.Items.Add(objResult.Properties["cn"][0]);

What am I doing wrong here...
 
I can get this to work perfectly in a winform. But when I want to do it from
a webform it does not work. I am getting 2 error messages focused on the last
line of code

The best overloaded method match for
'system.web.ui.webcontrols.listitemcollection.Add(string)' has some invalid
arguements

and secondly

Arguement 1 cannot convert from object to string.
 
How whoul I do this to list a OU in the dropdown box...

I have script that can reboot a active directoy ou but I need my program to list the ou realtime and the give the defined pathof that ou so a administrator can pick the ou just like if he was in tha admin mmc any idea how to do this with manualy entering this info..... thanks
 
example....

TESTOU ----root ou for my org
----SUBOU1<--------------need to show up in drop down nav
------SUBOU2 and so on
 
Back
Top