kscott
kscott

Reputation: 1946

How to get streetaddress property of an organizational unit in windows active directory with LDAP in C#.Net

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(); }
  }
}

Upvotes: 1

Views: 4534

Answers (3)

geoffc
geoffc

Reputation: 4100

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).

Upvotes: 0

Per Noalt
Per Noalt

Reputation: 5102

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.

Upvotes: 3

benPearce
benPearce

Reputation: 38333

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.

Upvotes: 1

Related Questions