I have tried to update a employee photo in AD using the code below, but I received an error message said:
System.DirectoryServices.DirectoryServicesCOMException: ‘A constraint
violation occurred.00002082: AtrErr: DSID-03151EFE, #1: 0: 00002082: DSID-03151EFE,
problem 1005 (CONSTRAINT_ATT_TYPE), data 0, Att 160023
(thumbnailPhoto):len 181708
I have checked I can make modification in the photo employees because I am using an external tool to do it with my account. And I have used this approach to modify other fields of the user like email, or telephone number without any problem. I think the problem is between the assignment of the byte value to the field, but I am not sure. Thank you for your cooperation.
public static void UpdatePhoto(System.DirectoryServices.DirectoryEntry objADAM, string username)
{
DirectorySearcher query = new DirectorySearcher(objADAM);
query.PropertiesToLoad.Add("thumbnailPhoto");
query.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))";
SearchResult result = query.FindOne();
System.DirectoryServices.DirectoryEntry user = result.GetDirectoryEntry();
System.IO.FileStream inFile = new System.IO.FileStream("employeePhoto.PNG", System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] binaryData = new byte[inFile.Length]; // Read image into byte array
int bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
inFile.Close();
user.Properties["thumbnailPhoto"].Clear();
user.Properties["thumbnailPhoto"].Add(binaryData);
user.CommitChanges();
}