views:

206

answers:

1

I have this code currently,

        string defaultNamingContext;

        DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
        defaultNamingContext = rootDSE.Properties["defaultNamingContext"].Value.ToString();
        rootDSE = new DirectoryEntry("LDAP://" + defaultNamingContext);
        //DirectoryEntry domain = new DirectoryEntry((string)"LDAP://" + defaultNamingContext);

        DirectorySearcher ouSearch = new DirectorySearcher(rootDSE,"(objectCategory=Organizational-Unit)", 
                                 null, SearchScope.Subtree);

        MessageBox.Show(rootDSE.Path.ToString());
        try
        {
            SearchResultCollection collectedResult = ouSearch.FindAll();
            foreach (SearchResult temp in collectedResult)
            {
                comboBox1.Items.Add(temp.Properties["name"][0]);
                DirectoryEntry ou = temp.GetDirectoryEntry();
            }
        }

When i use the debugger i can see that rootDSE.Path is infact pointing to the right place, in this case DC=g-t-p,DC=Local but the directory searcher doesn't find any results. Can anyone help?

+2  A: 

Stephen - my bad - for some reason, the search using objectCategory doesn't work.

Even though the objectCategory is displayed as CN=Organizational-Unit, for searching, you still need to use the same value as for the objectClass:

So try to use the filter (objectCategory=organizationalUnit) - that definitely works for me!

UPDATE: in order to get some properties in your search result (in order to display them in the combo box), you need to include those when you create the DirectorySearcher:

DirectorySearcher ouSearch = new DirectorySearcher(rootDSE);
ouSearch.Filter = "(objectCategory=Organizational-Unit)";
ouSearch.SearchScope = SearchScope.Subtree;

ouSearch.PropertiesToLoad.Add("name");
// add more properties if you want to ...

With this, you should definitely be able to grab the temp.Properties["name"][0] and stick it into the combobox's list of items.

I don't really see what you need the line

DirectoryEntry ou = temp.GetDirectoryEntry();

after grabbing the name property .....

marc_s
OMG something so simple, kicking myself for not trying that, after my few hours of trial and error coding :@I don't think its the most productive i've been recently...
Stephen Murby
Does it look like i am collecting the search objects correctly to add them to the Combo box items?
Stephen Murby
+1 After having tested it myself and about to write my answer, I came to the same exact conclusion.
Will Marcouiller
@Stephen Murby: updated my answer - I guess you'll have to specify what properties to include in the search results, otherwise you'll only have the .Path property, I think
marc_s
Thanks both you guys, help greatly appreciated!!!Sorry to be a pain, the main issue i have with the Combobox is it throwing an unhandled COMException "Unknown Error (0x80005000)", i figured with it being an unknown error i was definitely doing something studpid.
Stephen Murby
Sorry Marc, I have already amended the PropertiesToLoad("name"), but i still get the unknown exception. Infact this particular exception has only begun occurring since i amended the original source. I will find the line where it threws the exception tomorrow, unfortunately i can run the app at home.
Stephen Murby