Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. c# Search in root/subtree active directory users

c# Search in root/subtree active directory users

Scheduled Pinned Locked Moved C#
csharpwindows-adminhelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jwradhe
    wrote on last edited by
    #1

    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.company

    I 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);
            }
        }
    
    Richard Andrew x64R 1 Reply Last reply
    0
    • J jwradhe

      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.company

      I 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);
              }
          }
      
      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups