VB.NET Select Active Directory Container Dialog

November 10, 2011 — 16 Comments

For any .NET developers writing programs that require a user to select a container or OU from Active Directory, I’ve made an easy to use dialog window that will show the domain tree and let the user select a specific container.

You can download the full source code for the dialog window along with an example application that shows how to use it, here:


and here’s a screenshot:

Using it is as simple as:

Dim SelectContainerDialog As New Cjwdev.ActiveDirectory.Dialogs.SelectAdContainerDialog
If SelectContainerDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
End If

Hope it helps other developers out Smile


16 responses to VB.NET Select Active Directory Container Dialog


    Is there a way that this app or any of your apps display opened file on a specific server and the username of the person who has it open?


      No I’ve never bothered making such an app because its pretty easy to just use the Computer Management MMC snap-in to find that out (by going to the Shared Folders section then Open Files, from the file server)


        Thanks for the quick reply. The intent was to have my app get (on request) the “username” of the person who has a file opened. Not everyone has access to the server so it’s a bit difficult what you suggest. I’ll keep looking but you did point me in the right location. Thanks.


        Ah right, I thought you meant for an Admin. For end users it is tricky because they don’t have the required permissions. If you’re actually writing an app then you need to use the NetFileEnum function (native Windows API) but like I said only admins or members of the server operators group have permission to execute this http://msdn.microsoft.com/en-us/library/windows/desktop/bb525378(v=vs.85).aspx


    Thanks for the quick reply. The intent was to have my app get (on request) the “username” of the person who has a file opened. Not everyone has access to the server so it’s a bit difficult what you suggest. I’ll keep looking but you did point me in the right location. Thanks.



    Finally I found the tool that I’m looking for a long time. I’m novice on VB. Can you share WPF version of the Active Directory Treeview code if you don’t mine?

    Thank you again!


    Is there a way to implement Single Sign On with your code. Currently when you open the SelectContainerDialog you have to pass the DomainName, Username, and Password. I can automatically feed the Current logged in user’s Domain and Username with Environment.UserDomain\Environment.Username, but I’m not aware of a way to pass the password. Any ideas?


      I’m not sure why you think you have to specify a domain and credentials. That’s an option, you don’t HAVE to do it. See my example code in this very blog post – just don’t set the domain name or username or password and it will use the current user’s domain name and logon credentials


    cjwdev, never mind.
    I watched another one of your videos where you use this dialog and it said that if you don’t pass the Domain, Username. and Password, then it uses current users credentials (SSO) automatically. Thanks again for this great bit of code.


    I am brand new to programming. I am basically learning as I go, by online research and reading books. I have created a program that I use at work, to create a list of random names. So far, I have been taking the list of names and using it to create users in AD with exchange mailboxes through PowerShell scripting. Ideally, I would like my program to be able to connect to AD to create the users/mailboxes and skip the PowerShell script altogether. This article appealed to me because I think it may help with selecting the OU that the users should be created in. But, being new, I am not sure how to add this to my code. I coded in visual studio 2010, with visual basic. Any help/guidance you can give would be greatly appreciated.


      Unfortunately if you’re working with Exchange then you probably won’t have much choice but to carry on using Powershell. As far as I know there are no pure .NET ways of interacting with Exchange servers. There’s a .NET API that lets you access emails from user’s mailboxes and create calendar items etc but I’ve not seen anything that lets you actually work from a server side and create mailboxes, change server options, etc… which is really annoying considering MS push .NET as their language/framework of choice, and a lot of Exchange is actually written in .NET now, but yet they provide no way for .NET developers to interact with it.
      Anyway, on to your question about how to use the code. Just download the project files I posted on vbforums and open them – then build the solution (e.g use the Build menu in Visual Studio) and that will compile the code into a DLL in the bin\Release folder within those project files (or bin\Debug if you’ve left the solution in debug build mode). Now you can add a reference to this DLL to your program by opening your program project in Visual Studio, right clicking on your project name on the right hand side, go to Add Reference, browse for the DLL you compiled in the previous steps, and that’s it. Once you’ve done that you can use the code I posted in this blog post to create and show the OU window.
      PS. I would highly recommend that vbforums.com forum for general VB.NET questions, I learnt a lot from there when I was starting out:

    Srinath Sadda July 30, 2013 at 15:41

    Thanks for sending me the WPF version of the application a long time back. I encountered an issue while I’m signing the assembly. It’s complaining about strong name referenced in the assembly (ADTreeViewWpf.dll)

    Once user selects OU, it shows selected contain Distinguished Name (DN). How can we get the value in behind the scenes? I’m trying to call the “SelectContainerWindow” from other .NET application. Based on user selected OU (and clicks OK), I’m trying to pass the selected value to a variable or other application as parameter.

    Also it would be nice to see web based version of AD Info and Tidy tools like Manage Engine software. In our organization, we’re looking for web based AD tools.

    Any suggestions would be greatly appreciated.


      I already showed how to do that in this blog post (see the code example in red below the screenshot). The property that contains the DN for the OU the user selected is called SelectedContainerPath (or in the WPF version I think I just named it SelectedContainer)


    Hello Chris.
    Thank you so much for sharing this work. You are an amazing person.
    This code is very well written. The year before last i downloaded the ADTreeViewCntl 1.5 and stored it for future work. And today that future is here, and I noticed that you have made new version where it is all in a form. Many methods updated to new improved dot.net notations. Brilliant. Mozart-ish.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s