Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com

I am retrieving some info from Active directory and  I had a question what happens if invalid (non-existing) Property Name to System.DirectoryServices.PropertyCollection.Item  will be passed

E.g -what  would happened if  the code reads  anEntry.Properties["userAccountControl"].Value ,but DirectoryEntry doesn't have property "userAccountControl“ .

It is not documented in MSDN  so I had to investigate it myself.

In .Net 1.1 I found that anEntry.Properties["userAccountControl”] returns not null PropertyValueCollection object, but prop.Value is null.

So the safe code to retrieve properties value should be similar the following:

      int val = 0;
      object oVal =  anEntry.Properties["userAccountControl"].Value;
      if (null!= oVal) //if property doesn't exist, than value is null
      {
          val = (int) oVal;
      }
      else   Debug.Assert(false,"anEntry.Properties[\"userAccountControl\"] not found");

Posted on Wednesday, August 10, 2005 9:28 AM Active Directory | Back to top


Comments on this post: System.DirectoryServices.PropertyCollection.Item -what returns if propertyName is invalid

# re: System.DirectoryServices.PropertyCollection.Item -what returns if propertyName is invalid
Requesting Gravatar...
There are a few object types where this property will always have a value, and others where it will be null - it depends on what you are doing.

If you are trying, for example, to read in a collection of all objects from AD, you have to test this property in a try/catch because it will obviously fail on certain object types.

If you are working only with User, Domain Controller, Server, Computer objects - you should not see this null/blank at any time.

Other objects you will find it null/not-set.

HTH's...
Left by Michael Hamilton on Sep 07, 2005 3:10 AM

# re: System.DirectoryServices.PropertyCollection.Item -what returns if propertyName is invalid
Requesting Gravatar...
"If you are working only with User, Domain Controller, Server, Computer objects - you should not see this null/blank at any time. "

Michael, I do see it null for my domain AD users.

Here is my code,

dirEntryPC.Path = "WinNT://" + txtDomain.Text + "/" + txtComputer.Text;

DirectoryEntries entries = dirEntryPC.Children;
DirectoryEntry usr = entries.Find(selNode.Text, "User");
object oVal = usr.Properties["userAccountControl"].Value;
if (null!= oVal){
int val = (int) oVal;
usr.Properties["userAccountControl"].Value = val | ADS_UF_ACCOUNTDISABLE;
usr.CommitChanges();
}
else System.Diagnostics.Debug.Assert(false, "[\"userAccountControl\"] not found");

Left by Syed Irtaza Ali on Dec 22, 2005 7:24 PM

# re: System.DirectoryServices.PropertyCollection.Item -what returns if propertyName is invalid
Requesting Gravatar...
I didn't see null userAccountControl values for User objects in my development domain,
but one of our customers had exceptions before I changed code to handle nulls as described in this post.
Left by Michael Freidgeim on Dec 23, 2005 5:47 AM

# Print UserAccountControl AD property programmatically.
Requesting Gravatar...
How to Print UserAccountControl AD property programmatically.
Left by Michael Freidgeim on Dec 08, 2006 9:25 AM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net