Active Directory OU Retreival

M

multisync

I have an OU called "Sites"

I want to click a button called btn1 and I want to retrieve all the Sites
under this OU into a dropdownlist Called dropdownlist1. I want to do this
using LDAP. I'm working with a asp.net webform.

Can someone please provide me the code?
 
M

Marc Scheuner

I have an OU called "Sites"

Do you know the full LDAP path for that OU as well??

It would have to be something like:

LDAP://OU=Sites,OU=SomeOtherOU,dc=YourCompany,dc=com
I want to click a button called btn1 and I want to retrieve all the Sites
under this OU into a dropdownlist Called dropdownlist1. I want to do this
using LDAP. I'm working with a asp.net webform.

Using that full LDAP path for your main "Sites" OU, you can easily set
up a DirectorySearcher to retrieve all the sub-OU's under that OU. Do
you want to get just the immediate children, or do you want to recurse
down through the OU hierarchy?

This assumes you want only the immediate children - and only of type
"organizationalUnit":

DirectoryEntry deMySites = new
DirectoryEntry("LDAP://OU=Sites,OU=SomeOtherOU,dc=YourCompany,dc=com");

DirectorySearcher dsSubOUs = new DirectorySearcher(deMySites);

// search only immediate children - use SearchScope.Subtree for all
// immediate children and subsequent grandchildren etc.
dsSubOUs.SearchScope = SearchScope.OneLevel;

// set the filter - we're only interested in OU's
dsSubOUs.Filter = "(objectClass=organizationalUnit)";

// set up what attributes to load - specify whatever attributes
// you are interested in as results in your search
dsSubOUs.PropertiesToLoad.Add("name");
dsSubOUs.PropertiesToLoad.Add("cn");
dsSubOUs.PropertiesToLoad.Add("dateCreated");

// search for all matching OU's
foreach(SearchResult oResult in dsSubOUs.FindAll())
{
// add the "name" attribute of the result to your combo box
if(oResult.Properties.Contains("name"))
{
childOUsComboBox.Items.Add(oResult.Properties["name"][0].ToString();
}
}


Hope this helps
Marc
 
M

multisync

Yes, its something like that.

Marc Scheuner said:
I have an OU called "Sites"

Do you know the full LDAP path for that OU as well??

It would have to be something like:

LDAP://OU=Sites,OU=SomeOtherOU,dc=YourCompany,dc=com
I want to click a button called btn1 and I want to retrieve all the Sites
under this OU into a dropdownlist Called dropdownlist1. I want to do this
using LDAP. I'm working with a asp.net webform.

Using that full LDAP path for your main "Sites" OU, you can easily set
up a DirectorySearcher to retrieve all the sub-OU's under that OU. Do
you want to get just the immediate children, or do you want to recurse
down through the OU hierarchy?

This assumes you want only the immediate children - and only of type
"organizationalUnit":

DirectoryEntry deMySites = new
DirectoryEntry("LDAP://OU=Sites,OU=SomeOtherOU,dc=YourCompany,dc=com");

DirectorySearcher dsSubOUs = new DirectorySearcher(deMySites);

// search only immediate children - use SearchScope.Subtree for all
// immediate children and subsequent grandchildren etc.
dsSubOUs.SearchScope = SearchScope.OneLevel;

// set the filter - we're only interested in OU's
dsSubOUs.Filter = "(objectClass=organizationalUnit)";

// set up what attributes to load - specify whatever attributes
// you are interested in as results in your search
dsSubOUs.PropertiesToLoad.Add("name");
dsSubOUs.PropertiesToLoad.Add("cn");
dsSubOUs.PropertiesToLoad.Add("dateCreated");

// search for all matching OU's
foreach(SearchResult oResult in dsSubOUs.FindAll())
{
// add the "name" attribute of the result to your combo box
if(oResult.Properties.Contains("name"))
{
childOUsComboBox.Items.Add(oResult.Properties["name"][0].ToString();
}
}


Hope this helps
Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top