Problems with DirectoryEntry

D

Drew Burchett

I am working with Active Directory in C# and trying to read and set certain
attributes. To set an attribute, I have the following code:

DirectoryEntry de = new DirectoryEntry(); //This is assigned to a
DirectoryEntry from a search result.

de.Properties["telephoneNumber"].Value = telephoneNumber;
de.CommitChanges();

The code above works perfectly unless telephoneNumber is an empty string.
How can I either assign an empty string to the value, or remove any value
that is there?
 
W

Willy Denoyette [MVP]

Drew Burchett said:
Thank you, but that didn't seem to work either.

Colby Africa said:
How about de.Properties["telephoneNumber"].Clear?

Colby

Are you sure that this (optional) property exists, you cant change the
value of a non-existing property, you have to add it first like this:

de.Properties["telephoneNumber"].Add(""); // add property and set it's
value to an empty string.

Willy.
 
D

Drew Burchett

Yes. I've used ADSI edit to go into the Active Directory and see for
certain that the property exists.

Also, in stepping through the code, I found that setting the property to
empty or calling Clear either one will remove the property, but it appears
that it never gets saved when I call CommitChanges. Not sure what would be
causing that since CommitChanges never throws an exception and won't return
any error codes.

Willy Denoyette said:
Drew Burchett said:
Thank you, but that didn't seem to work either.

Colby Africa said:
How about de.Properties["telephoneNumber"].Clear?

Colby

Are you sure that this (optional) property exists, you cant change the
value of a non-existing property, you have to add it first like this:

de.Properties["telephoneNumber"].Add(""); // add property and set it's
value to an empty string.

Willy.
 
W

Willy Denoyette [MVP]

Drew Burchett said:
Yes. I've used ADSI edit to go into the Active Directory and see for
certain that the property exists.

Also, in stepping through the code, I found that setting the property to
empty or calling Clear either one will remove the property, but it appears
that it never gets saved when I call CommitChanges. Not sure what would
be causing that since CommitChanges never throws an exception and won't
return any error codes.

Willy Denoyette said:
Drew Burchett said:
Thank you, but that didn't seem to work either.

How about de.Properties["telephoneNumber"].Clear?

Colby

Are you sure that this (optional) property exists, you cant change the
value of a non-existing property, you have to add it first like this:

de.Properties["telephoneNumber"].Add(""); // add property and set it's
value to an empty string.

Willy.

Ok I see, the thelephoneNumber cannot be an empty string, *empty* strings
(strings with 0 length) are not valid attribute in AD, that means that when
you set a value to "" , that the property gets deleted.
So, you need to set the value to a valid number or set it to a single space
" " or whatever value that is indicative for a non valid number. Much better
IMO is to delete the property if no value exists for the property.

Willy.
,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top