M
musosdev
Hi there
I'm writing a peice of code to retrieve the ipPhone field for a given AD
user. However, some of our users don't have an ipPhone, so I need to be able
to check whether this field is null (currently, trying to retrieve a
non-existent ipPhone is throwing an error).
Sadly, I can't for the life of me work out how to add a null check on the
PropertyValueCollection.
(the code works when the ipPhone field has a value)
here's the code...
string adPath = "LDAP://thepoint.org.uk.local"; //
+ConfigurationManager.AppSettings["DefaultADServer"];
DirectoryEntry adEntry = new DirectoryEntry(adPath);
DirectorySearcher adSearch = new DirectorySearcher(adEntry);
adSearch.Filter = "(SAMAccountName=" + userSAM + ")";
adSearch.PropertiesToLoad.Add("AllProperties");
SearchResult adResult = adSearch.FindOne();
if (adResult != null)
{
adEntry = adResult.GetDirectoryEntry();
if (adEntry.Properties["ipPhone"] != null)
{
pExt = adEntry.Properties["ipPhone"][0].ToString(); //
GetPropertyValue(adEntry, "ipPhone");
}
}
else { pExt = userSAM; }
As you can see, I've tried checking adEntry.Properties["ipPhone"] != null
and also ["ipPhone"][0] for null too.
I'm sure it's obvious to someone who works with DirectoryServices all the
time, but I just can't find it!
Cheers
Dan
I'm writing a peice of code to retrieve the ipPhone field for a given AD
user. However, some of our users don't have an ipPhone, so I need to be able
to check whether this field is null (currently, trying to retrieve a
non-existent ipPhone is throwing an error).
Sadly, I can't for the life of me work out how to add a null check on the
PropertyValueCollection.
(the code works when the ipPhone field has a value)
here's the code...
string adPath = "LDAP://thepoint.org.uk.local"; //
+ConfigurationManager.AppSettings["DefaultADServer"];
DirectoryEntry adEntry = new DirectoryEntry(adPath);
DirectorySearcher adSearch = new DirectorySearcher(adEntry);
adSearch.Filter = "(SAMAccountName=" + userSAM + ")";
adSearch.PropertiesToLoad.Add("AllProperties");
SearchResult adResult = adSearch.FindOne();
if (adResult != null)
{
adEntry = adResult.GetDirectoryEntry();
if (adEntry.Properties["ipPhone"] != null)
{
pExt = adEntry.Properties["ipPhone"][0].ToString(); //
GetPropertyValue(adEntry, "ipPhone");
}
}
else { pExt = userSAM; }
As you can see, I've tried checking adEntry.Properties["ipPhone"] != null
and also ["ipPhone"][0] for null too.
I'm sure it's obvious to someone who works with DirectoryServices all the
time, but I just can't find it!
Cheers
Dan