c# Search in root/subtree active directory users
-
Hi!
Im having some problem in my function to search on all users in my active directory, root and subtree.my ad is like this:
ad.company
-> se.ad.company
-> fin.ad.company
-> no.ad.companyI can search , but only get some results from ad.company, not the other subtree directorys.
What have i done wrong?And this is my code:
private void AutoCompleteTextBox_ReferenceUser()
{try { var attributeName = "sn"; string OU = "DC=ad,DC=company"; var searchString = textBox\_manager.Text; var ent = new DirectoryEntry("GC://" + OU); var mySearcher = new DirectorySearcher(ent); mySearcher.Filter = string.Format("(&(anr={0})(objectCategory=user)(objectClass=user))", attributeName, searchString); SearchResultCollection result = mySearcher.FindAll(); List names = new List(); foreach(SearchResult sr in result) { var n = sr.Properties\["cn"\]\[0\].ToString(); if (!names.Contains(n)) { stringCollection.Add(n); } } textBox\_referenceuser.AutoCompleteMode = AutoCompleteMode.Suggest; textBox\_referenceuser.AutoCompleteSource = AutoCompleteSource.CustomSource; textBox\_referenceuser.AutoCompleteCustomSource = stringCollection; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
-
Hi!
Im having some problem in my function to search on all users in my active directory, root and subtree.my ad is like this:
ad.company
-> se.ad.company
-> fin.ad.company
-> no.ad.companyI can search , but only get some results from ad.company, not the other subtree directorys.
What have i done wrong?And this is my code:
private void AutoCompleteTextBox_ReferenceUser()
{try { var attributeName = "sn"; string OU = "DC=ad,DC=company"; var searchString = textBox\_manager.Text; var ent = new DirectoryEntry("GC://" + OU); var mySearcher = new DirectorySearcher(ent); mySearcher.Filter = string.Format("(&(anr={0})(objectCategory=user)(objectClass=user))", attributeName, searchString); SearchResultCollection result = mySearcher.FindAll(); List names = new List(); foreach(SearchResult sr in result) { var n = sr.Properties\["cn"\]\[0\].ToString(); if (!names.Contains(n)) { stringCollection.Add(n); } } textBox\_referenceuser.AutoCompleteMode = AutoCompleteMode.Suggest; textBox\_referenceuser.AutoCompleteSource = AutoCompleteSource.CustomSource; textBox\_referenceuser.AutoCompleteCustomSource = stringCollection; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
jwradhe wrote:
I can search , but only get some results from ad.company, not the other subtree directorys.
This statement doesn't make sense to me, because all of the subtrees you've listed contain "ad.company" as part of their names.
The difficult we do right away... ...the impossible takes slightly longer.