views:

614

answers:

3

Each of our users is assigned to a primary organizational unit (OU) based on which global office they are in. So the "Chicago" OU contains all the associates in our Chicago office.

Using c# and .net 3.5, my task is to extract all of these users.

Unless the users are in a satellite or home office, their street address, city, state, etc. are empty, but the OU contains these details. When in Windows' Active Directory interface, right clicking on the OU and selecting properties gives a place to put all of this information just as on a user. However, when I try to access these properties like I do a user, I get an object reference error, suggesting these attributes do not exist the same way for an OU that they do for a user.

How do/can I access these location parameters from an OU object?

Here is a sample of the code I am using, showing streetaddress as an example, the statement trying to assign the value of streetaddress from the OU fails, where the assignment from associate succeeds.

foreach (SearchResult subOU in results)
{
   ResultPropertyValueCollection subColl = subOU.Properties["distinguishedname"];
   string subPath = subColl[0].ToString();
   DirectoryEntry subEntry = new DirectoryEntry("LDAP://" + subPath);
   DirectorySearcher userSearcher = new DirectorySearcher(subEntry);
   userSearcher.SearchScope = SearchScope.OneLevel;
   userSearcher.Filter = "(objectClass=user)";
   foreach (SearchResult user in userSearcher.FindAll())
   {
     ResultPropertyValueCollection userColl = user.Properties["distinguishedname"];
     string userPath = userColl[0].ToString();
     DirectoryEntry userEntry = new DirectoryEntry("LDAP://" + userPath);
     PropertyCollection associateProperties = userEntry.Properties;
     PropertyCollection ouProperties = subEntry.Properties;

     string streetAddress = string.Empty;
     if (associateProperties["streetaddress"].Value == null) 
     { streetAddress = ouProperties["streetaddress"].Value.ToString(); }
     else
     { streetAddress = associateProperties["streetaddress"].Value.ToString(); }
  }
}
A: 

To avoid the ObjectReference exception you should check the collection contains the required attribute using the Contains(string) method. See here

I believe that AD will only stored valued attributes on an object, if a particular attribute has never been assigned a value it won't be available.

benPearce
Yes, certainly this would help to avoid triggering the object reference error since you are correct in that the property would indeed not be part of the object if it had no value. In this case, the object is known to have a value for that field, just the syntax of that field is being used incorrectly, which is the larger problem that would need to be addressed. Thank you for your answer.
kscott
+1  A: 

If you change the Street-field on the General-tab in Active Directory Users & Computers for a user the value is stored in the streetAddress-attribute in the directory. If however you change the same field for an OU that value is stored in the street-attribute of that OU in the directory.

This is because OU objects are not (as defined in the Active Directory default schema) permitted to contain the streetAddress-attribute.

So (not having analyzed your code further) if you change ouProperties["streetaddress"] to ouProperties["street"] you'll might get the result you're looking for.

Per Noalt
Do you know of an exhaustive list of properties of these objects? I have not had much luck finding such a list. I imagine there are other important differences between users and OU properties.
kscott
Per Noalt
A: 

I found the AD schema references at:

http://download.microsoft.com/download/a/e/6/ae6e4142-aa58-45c6-8dcf-a657e5900cd3/%5BMS-ADA1%5D.pdf A-L http://download.microsoft.com/download/a/e/6/ae6e4142-aa58-45c6-8dcf-a657e5900cd3/%5BMS-ADA2%5D.pdf Just M http://download.microsoft.com/download/a/e/6/ae6e4142-aa58-45c6-8dcf-a657e5900cd3/%5BMS-ADA3%5D.pdf N-Z http://download.microsoft.com/download/a/e/6/ae6e4142-aa58-45c6-8dcf-a657e5900cd3/%5BMS-ADTS%5D.pdf AD technical info

That would answer this question for you.

Also, the Win2K8 ADUC MMC snapin if you go to View, select Advanced Features, (enable the tick) then you get the Attribute Editor. (Something ConsoleOne for eDirectory has had for probably close to a decade now!).

One small note, in AD schema, first character is always lower case, and I run at sufficiently high res that the lower case L's are hard to see as L's. (Need a better screen font, but mea culpa).

geoffc