I am trying to update a photo for the employees in AD using the following code:
public static void AddPictureToUser(string strFileName)
{
// Open file
System.IO.FileStream inFile = new System.IO.FileStream(strFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
// Retrive Data into a byte array variable
byte[] binaryData = new byte[inFile.Length];
inFile.Close();
string ldap = "LDAP://COMPANY.COM:389";
string userAuthToMakeChanges = "user";
string passwordUserAuthToMakeChanges = "password";
string employeeUsername = "usernameToFind";
System.DirectoryServices.DirectoryEntry directoryEntry = new System.DirectoryServices.DirectoryEntry(ldap, userAuthToMakeChanges, passwordUserAuthToMakeChanges, AuthenticationTypes.Secure);
DirectorySearcher query = new DirectorySearcher(directoryEntry);
query.PropertiesToLoad.Add("thumbnailPhoto");
query.Filter = "(&(objectClass=user)(sAMAccountName=" + employeeUsername + "))";
SearchResult result = query.FindOne();
System.DirectoryServices.DirectoryEntry user = result.GetDirectoryEntry();
// Clear existing picture if exists
user.Properties["thumbnailPhoto"].Clear();
// Update attribute with binary data from file
user.Properties["thumbnailPhoto"].Add(binaryData);
user.CommitChanges();
}
But I am receiving this error: System.DirectoryServices.DirectoryServicesCOMException: ‘A constraint violation occurred.’
No more details
I don’t think the problem is related with the access because I can change the employee data with though this tool. https://www.codetwo.com/freeware/active-directory-photos/?sts=1327 What do you think is happening? Thanks in advanced!