• 0

Update managers name in Active Directory


Question

I am trying to update the managers name in active directory with an application I got that updates active directory.

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:

using (DirectoryEntry manager = new DirectoryEntry(GetDirectoryEntry()))

using (DirectoryEntry minion = new DirectoryEntry(GetDirectoryEntry())) {

minion.Properties["manager"].Value =

manager.Properties["distinguishedName"].Value.ToString();

}

Error: The value provided for adsObject does not implement IADs.

With this is one:

if (oEmployee.Manager != String.Empty) {

dentry.Properties["manager"].Value = "CN=oEmployee.Manager,DC=domain, DC=net";

Error: System.DirectoryServices.DirectoryServicesCOMException (0x8007202F): A constraint violation occurred.(Exception from HRESULT: 0x8007202F)

And with this one:

if (oEmployee.Manager != String.Empty) {

dentry.Properties["manager"].Value = oEmployee.Manager;

Error: System.DirectoryServices.DirectoryServicesCOMException (0x8007202F): A constraint violation occurred.(Exception from HRESULT: 0x8007202F)

This is my code:

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();

}

}

}

}

Any ideas of how I can update the manager name?

Thanks.

0 answers to this question

Recommended Posts

There have been no answers to this question yet

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.