Getting the parent organization unit (if any) of an organizational unit in Active Directory
I've created a small entity class:
public class OrganizationalUnit
{
public string Name { get; set; }
public string ParentUO { get; set; }
public string Path { get; set; }
}
Here's how I create objects of this type:
/// <summary>
/// Provides an object that allows you to get organizational units within an
/// active directory domain.
/// </summary>
/// <param name="connectionString">The LDAP connection string to a domain.
/// For example LDAP://DC=YourCompany,DC=com</param>
public ActiveDirectoryOrganizationalUnitRepository(string connectionString, string username, string password)
{
organizationalUnits = new List<OrganizationalUnit>();
if (DomainExists(connectionString))
{
var baseDirectory = new DirectoryEntry(connectionString);
baseDirectory.Username = username;
baseDirecto开发者_如何转开发ry.Password = password;
DirectorySearcher searcher = new DirectorySearcher();
searcher.SearchRoot = baseDirectory;
searcher.Filter = "(objectCategory=organizationalUnit)";
searcher.SearchScope = SearchScope.Subtree;
var ouResults = searcher.FindAll();
foreach (SearchResult ou in ouResults)
{
organizationalUnits.Add(new OrganizationalUnit() {
Path = ou.Path,
Name = ou.Properties["name"][0].ToString(),
ParentUO = ou.Properties["parent"][0].ToString()}
);
}
}
}
I need help filling the ParentOU property. I get a index out of range exception when trying to get it:
ParentUO = ou.Properties["parent"][0].ToString();
So this means, there is no property named 'parent'.
Any suggestions? I'd also like to find a list of properties that exist, but I've yet to find one online.
First :
From the pure Directory point of vue, you've got in your organizationalUnit (OU) an attribute called "distinguishedName
" which looks like :
OU=currentOU,OU=parentOU,...,DC=domain,DC=..
so you can easily compute the string of the parent OU.
Second :
On the programming point of view, you've got a property of DirectoryEntry
class which is parent
. Here is a sample code.
String myADSPath = "LDAP://onecity/CN=user,CN=Users,DC=onecity,DC=corp,DC=fabrikam,DC=com";
DirectoryEntry myDirectoryEntry=new DirectoryEntry(myADSPath, UserName, SecurelyStoredPassword);
Console.WriteLine("Parent is :"+myDirectoryEntry.Parent.Path);
JP
I found this code which lists all the properties a SearchResult object may have. It seems it doesn't list it's parent organizational unit.
DirectorySearcher searcher = new DirectorySearcher();
searcher.SearchRoot = baseDirectory;
searcher.Filter = "(objectCategory=organizationalUnit)";
searcher.SearchScope = SearchScope.Subtree;
var ouResults = searcher.FindAll();
foreach (SearchResult ou in ouResults)
{
ResultPropertyCollection myResultPropColl;
myResultPropColl = ou.Properties;
Console.WriteLine("The properties of the " +
"'mySearchResult' are :");
foreach (string myKey in myResultPropColl.PropertyNames)
{
string tab = " ";
Console.WriteLine(myKey + " = ");
foreach (Object myCollection in myResultPropColl[myKey])
{
Console.WriteLine(tab + myCollection);
}
}
}
Did you try DirectoryEntry.Parent?
The following code should work but I didn't try it.
organizationalUnits.Add(new OrganizationalUnit() {
Path = ou.Path,
Name = ou.Properties["name"][0].ToString(),
ParentUO = ou.GetDirectoryEntry().Parent.Path}
There is no attribute called "Parent" on the organizationUnit class object. Please check MSDN for all the attributes on organizational-Unit.
精彩评论