I been strugling with this for a few weeks now and couldn't find the best way to update the managers name.
I got a datareader that returns all the employees from the database then a method checks each one of the records and compares them in AD and them commits the changes to the users information.
This is what I have try so far:
This is one updates the other properties except the managers name:
Expand|Select|Wrap|Line Numbers
- using (DirectoryEntry manager = new DirectoryEntry(GetDirectoryEntry()))
- using (DirectoryEntry minion = new DirectoryEntry(GetDirectoryEntry())) {
- minion.Properties["manager"].Value =
- manager.Properties["distinguishedName"].Value.ToString();
- }
With this is one:
Expand|Select|Wrap|Line Numbers
- if (oEmployee.Manager != String.Empty) {
- dentry.Properties["manager"].Value = "CN=oEmployee.Manager,DC=domain, DC=net";
And with this one:
Expand|Select|Wrap|Line Numbers
- if (oEmployee.Manager != String.Empty) {
- dentry.Properties["manager"].Value = oEmployee.Manager;
This is my code:
Expand|Select|Wrap|Line Numbers
- public void ModifyUsers(SortedList<string , clsEmployee> colEmployee) {
- clsEmployee oEmployee;
- DirectoryEntry dentry;
- string empId;
- DirectoryEntry de = GetDirectoryEntry();
- DirectorySearcher dirsearcher = new DirectorySearcher(de);
- dirsearcher.PageSize = 1000;
- dirsearcher.Filter = "(&(objectCategory=person)(objectClass=user))";
- SearchResultCollection dsCol = dirsearcher.FindAll();
- foreach (SearchResult restEnt in dsCol) {
- dentry = restEnt.GetDirectoryEntry();
- if (dentry.Properties["employeeId"] != null) {
- if (dentry.Properties["employeeId"].Value != null) {
- empId = dentry.Properties["employeeId"].Value.ToString();
- oEmployee = new clsEmployee();
- try {
- oEmployee = colEmployee[empId];
- }
- catch {
- //do nothing
- }
- if (!oEmployee.empId.Equals(string.Empty)) {
- //Update the information
- try {
- if (oEmployee.Pager != String.Empty) {
- dentry.Properties["pager"].Value = oEmployee.Pager;
- }
- if (oEmployee.TelephoneNumber != String.Empty) {
- dentry.Properties["telephoneNumber"].Value = oEmployee.TelephoneNumber;
- }
- if (oEmployee.HomePhone != String.Empty) {
- dentry.Properties["homePhone"].Value = oEmployee.HomePhone;
- }
- if (oEmployee.Mobile != String.Empty) {
- dentry.Properties["mobile"].Value = oEmployee.Mobile;
- }
- if (oEmployee.Nextel != String.Empty) {
- dentry.Properties["ipphone"].Value = oEmployee.Nextel;
- }
- if (oEmployee.RadioId != String.Empty) {
- dentry.Properties["otherIpPhone"].Value = oEmployee.RadioId;
- }
- if (oEmployee.Department != String.Empty) {
- dentry.Properties["department"].Value = oEmployee.Department;
- }
- if (oEmployee.Manager != String.Empty) {
- dentry.Properties["manager"].Value = oEmployee.Manager;
- }
- using (DirectoryEntry entryManager = new DirectoryEntry(GetDirectoryEntry()))
- using (DirectoryEntry dirent = new DirectoryEntry(GetDirectoryEntry())) {
- dirent.Properties["manager"].Value =
- entryManager.Properties["manager"].Value = oEmployee.Manager;
- }
- //Update managers name
- //if (oEmployee.Manager != String.Empty) {
- // dentry.Properties["distinguishedName"].Value = oEmployee.Manager;
- //}
- }
- catch (Exception e) {
- //For debugging
- Console.WriteLine(e.Message);
- }
- dentry.CommitChanges();
- dentry.Close();
- }
- }
- }
- }
Thanks.