I am trying to add a property/attribute to a user entry in Active Directory. I don't have any trouble updating property values using the following code.
string LDAPString = "LDAP://DC=oc,DC=edu";
DirectoryEntry ou = new DirectoryEntry(LDAPString, "fakeUsername", "password");
DirectorySearcher searcher = new DirectorySearcher(ou);
searcher.Filter = "sAMAccountName=" + username;
SearchResult result = searcher.FindOne();
DirectoryEntry user = new DirectoryEntry(result.Path, "fakeUsername", "password");
user.Properties[propertyName].Value = propertyValue;
user.CommitChanges();
user.Dispose();
However when I try to add a new item and call CommitChanges()
it throws an error:
The specified directory service attribute or value does not exist.
The ExtendedErrorMessage says the following:
00000057: LdapErr: DSID-0C090B8A, comment: Error in attribute conversion operation, data 0, v1db1
string propertyName = "test";
string propertyValue = "testValue";
user.Properties[propertyName].Add(propertyValue);
user.CommitChanges();
I have a feeling I am missing something simple but I can't seem to figure it out.
I did not understand that typically a property/attribute does not appear unless it is populated with a value. As marc_s alluded to the property is already there in the schema you just have to populate it with a value.