System.DirectoryServices.PropertyCollection.Item -what returns if propertyName is invalid

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");

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Wednesday, August 10, 2005 9:28 AM
Print

Comments on this entry:

# re: System.DirectoryServices.PropertyCollection.Item -what returns if propertyName is invalid

Left by Michael Hamilton at 9/7/2005 3:10 AM
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...

# re: System.DirectoryServices.PropertyCollection.Item -what returns if propertyName is invalid

Left by Syed Irtaza Ali at 12/22/2005 7:24 PM
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");

# re: System.DirectoryServices.PropertyCollection.Item -what returns if propertyName is invalid

Left by Michael Freidgeim at 12/23/2005 5:47 AM
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.

# Print UserAccountControl AD property programmatically.

Left by Michael Freidgeim at 12/8/2006 9:25 AM
Gravatar
How to Print UserAccountControl AD property programmatically.

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910