J
jtwright
I'm trying to create an ASP.NET page that will read over my active
directory tree and show the top level OrganizationalUnit values. From
there I want the user to be able to click on the top level and be able
to click on an OrganizationalUnit and have that return the sublevel
OrganizationalUnits. Basically a drill through that works like the AD
treeview, except on a webpage.
What is happening is that I get a list of the top level results,
example:
NorthAmerica
Canada
DistributionCenters
When I click on DistributionCenters I want it to drill into the levels
below it such as:
Houston
Seattle
Birmingham
BatonRouge
Lubbock
Instead the page is reloading the top level. I would expect that the
code:
DirectorySearcher adsiSearcher = new DirectorySearcher(
"LDAP://OU=NorthAmerica,DC=noam,DC=companyname,DC=com");
would filter down to the NorthAmerica Level and show the children since
it is getting passed into the Searcher. Not the case... any clues, or
advice? I'm 2 days into building this site, the details are complete I
just need an entry point with this page.
Here is what I have:
private void Page_Load(object sender, System.EventArgs e)
{
string qstring = Server.UrlDecode(Request.QueryString.ToString());
if(qstring.Equals(string.Empty))
{
qstring = "dc=companyname,dc=com";
}
DisplayResults(qstring);
}
private void DisplayResults(string searchString)
{
// DirectoryEntry adsiRoot = new DirectoryEntry( "LDAP://rootdse" );
// string sDefaultNamingContext = (string) adsiRoot.Properties[
"defaultNamingContext" ][0];
//
DirectorySearcher adsiSearcher = new DirectorySearcher( "LDAP://" +
searchString);
adsiSearcher.SearchScope = SearchScope.OneLevel;
adsiSearcher.CacheResults = false;
adsiSearcher.Filter = "(objectClass=organizationalUnit)";
adsiSearcher.PropertiesToLoad.Add( "Name" );
adsiSearcher.PropertiesToLoad.Add( "displayName" );
adsiSearcher.PropertiesToLoad.Add( "distinguishedName" );
adsiSearcher.PropertiesToLoad.Add( "objectCategory" );
SearchResultCollection adsiResult = adsiSearcher.FindAll();
string sOutput = "";
foreach(SearchResult result in adsiResult)
{
switch (result.Properties["objectCategory"][0].ToString())
{
case "CN=Person,CN=Schema,CN=Configuration,DC=companyname,DC=com":
sOutput += "<a href='userdetail.aspx?" +
result.Properties["displayName"][0].ToString() + "' target='_blank'>" +
result.Properties["displayName"][0].ToString() + "</a><br>";
break;
case
"CN=Organizational-Unit,CN=Schema,CN=Configuration,DC=companyname,DC=com":
sOutput += "<a href='tryadsiviewer.aspx?" +
result.Properties["distinguishedName"][0].ToString() + "'
target='_blank'>" + result.Properties["Name"][0].ToString() +
"</a><br>";
break;
}
}
divOutput.InnerHtml = sOutput;
}
directory tree and show the top level OrganizationalUnit values. From
there I want the user to be able to click on the top level and be able
to click on an OrganizationalUnit and have that return the sublevel
OrganizationalUnits. Basically a drill through that works like the AD
treeview, except on a webpage.
What is happening is that I get a list of the top level results,
example:
NorthAmerica
Canada
DistributionCenters
When I click on DistributionCenters I want it to drill into the levels
below it such as:
Houston
Seattle
Birmingham
BatonRouge
Lubbock
Instead the page is reloading the top level. I would expect that the
code:
DirectorySearcher adsiSearcher = new DirectorySearcher(
"LDAP://OU=NorthAmerica,DC=noam,DC=companyname,DC=com");
would filter down to the NorthAmerica Level and show the children since
it is getting passed into the Searcher. Not the case... any clues, or
advice? I'm 2 days into building this site, the details are complete I
just need an entry point with this page.
Here is what I have:
private void Page_Load(object sender, System.EventArgs e)
{
string qstring = Server.UrlDecode(Request.QueryString.ToString());
if(qstring.Equals(string.Empty))
{
qstring = "dc=companyname,dc=com";
}
DisplayResults(qstring);
}
private void DisplayResults(string searchString)
{
// DirectoryEntry adsiRoot = new DirectoryEntry( "LDAP://rootdse" );
// string sDefaultNamingContext = (string) adsiRoot.Properties[
"defaultNamingContext" ][0];
//
DirectorySearcher adsiSearcher = new DirectorySearcher( "LDAP://" +
searchString);
adsiSearcher.SearchScope = SearchScope.OneLevel;
adsiSearcher.CacheResults = false;
adsiSearcher.Filter = "(objectClass=organizationalUnit)";
adsiSearcher.PropertiesToLoad.Add( "Name" );
adsiSearcher.PropertiesToLoad.Add( "displayName" );
adsiSearcher.PropertiesToLoad.Add( "distinguishedName" );
adsiSearcher.PropertiesToLoad.Add( "objectCategory" );
SearchResultCollection adsiResult = adsiSearcher.FindAll();
string sOutput = "";
foreach(SearchResult result in adsiResult)
{
switch (result.Properties["objectCategory"][0].ToString())
{
case "CN=Person,CN=Schema,CN=Configuration,DC=companyname,DC=com":
sOutput += "<a href='userdetail.aspx?" +
result.Properties["displayName"][0].ToString() + "' target='_blank'>" +
result.Properties["displayName"][0].ToString() + "</a><br>";
break;
case
"CN=Organizational-Unit,CN=Schema,CN=Configuration,DC=companyname,DC=com":
sOutput += "<a href='tryadsiviewer.aspx?" +
result.Properties["distinguishedName"][0].ToString() + "'
target='_blank'>" + result.Properties["Name"][0].ToString() +
"</a><br>";
break;
}
}
divOutput.InnerHtml = sOutput;
}