0

I'm working on a project where I have to fetch the users from a particular OU in Active Directory.

I use the Dropdown menu to store all the OUs present. As soon as the user selects a particular OU and clicks on the button the users available for that should be displayed in the textbox.

This is the code being used:

public MainWindow()
    {
        InitializeComponent();
        DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
        string defaultContext = rootDSE.Properties["defaultNamingContext"][0].ToString();
        DirectoryEntry domainRoot = new DirectoryEntry("LDAP://" + defaultContext);
        DirectorySearcher ouSearcher = new DirectorySearcher(domainRoot);
        ouSearcher.SearchScope = SearchScope.Subtree;
        ouSearcher.PropertiesToLoad.Add("ou");
        ouSearcher.PropertiesToLoad.Add("cn");
        ouSearcher.SearchScope = SearchScope.Subtree;
        ouSearcher.Filter = "(objectCategory=organizationalUnit)";

        try
        {
            comboBox1.SelectedIndex = 0;
            comboBox1.Items.Insert(0, "Select An OU");
            string ouName;
            foreach (SearchResult deResult in ouSearcher.FindAll())
            {
                ArrayList alObjects = new ArrayList();
                ouName = deResult.Properties["ou"][0].ToString();
                comboBox1.Items.Insert(1, ouName.ToString());
            }          
        }
        catch (Exception ex2)
        {
        }
    }

private void button1_Click(object sender, RoutedEventArgs e) //Error is present in this part
    {
        string selectou = comboBox1.SelectedValue.ToString();
        DirectoryEntry rootDSE = new DirectoryEntry("LDAP://" + selectou);
        string defaultContext = rootDSE.Properties["defaultNamingContext"][0].ToString(); //Here is the problem
        DirectoryEntry domainRoot = new DirectoryEntry("LDAP://" + selectou);
        DirectorySearcher ouSearcher = new DirectorySearcher(selectou);
        ouSearcher.SearchScope = SearchScope.Subtree;
        ouSearcher.PropertiesToLoad.Add("cn");
        ouSearcher.SearchScope = SearchScope.Subtree;
        ouSearcher.Filter = "(&(objectClass=user)(objectCategory=person))";
        foreach (SearchResult deResult in ouSearcher.FindAll())
        {
            ArrayList alObjects = new ArrayList();
            string dcName = deResult.Properties["cn"][0].ToString();
            textBox1.Text = textBox1.Text + dcName.ToString() + "\n";
        }
    }

Now the problem is occuring at button1_click function. For defaultcontext it throws the following error:

System.Runtime.InteropServices.COMException: The server is not operational.

I'm not able to figure out how to go about this error. Am I missing some kind of assemblies?

Esha
  • 391
  • 1
  • 9
  • 37

1 Answers1

0

Change this line

ouName = deResult.Properties["ou"][0].ToString();

to

ouName = deResult.Properties["distinguishedName"][0].ToString();

and I think you'll be fine

Brian Desmond
  • 4,473
  • 1
  • 13
  • 11
  • my error occurred on button click. that code is working fine. – Esha Jul 09 '14 at 07:32
  • The path you're passing in the button click code comes from what you're populating in the drop down list. The value you're putting in the drop down list isn't valid for binding. – Brian Desmond Jul 09 '14 at 15:33