I am developing a program that will update certain active directory properties using HR data (retrieved from SQL Server). The 1st stage was to match using values like samAccountName, email, surname etc. and that has been successful.
Now I need to change the filtering, so that I can retrieve the relevant Active Directory entry using employee number which I believe is employeeID in Active Directory. I have been trying code such as:
dirSearcher.Filter = String.Format("(&(objectCategory=user)(employeeid={0}))", employeeId);
src = dirSearcher.FindAll();
try
{
de = src[0].GetDirectoryEntry();
textBoxResult.Text = de.Guid.ToString();
}
catch (Exception ex)
{
textBoxResult.Text = ex.Message;
activeDirectoryID = "";
}
return activeDirectoryID;
However, src is always null.
I did manage to get it working using:
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, directoryRootPath, username, password);
UserPrincipal searchTemplate = new UserPrincipal(ctx);
searchTemplate.EmployeeId = HREmployeeNumber;
PrincipalSearcher ps = new PrincipalSearcher(searchTemplate);
UserPrincipal user = (UserPrincipal)ps.FindOne();
try
{
activeDirectoryID = user.Guid.ToString();
}
However, this code is a lot slower. Originally, when it was matching on samAccountName or email address it processed just over 4000 records in about 5 minutes. In principal the code works, but takes 40 minutes.
Any advice?