470,587 Members | 2,238 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,587 developers. It's quick & easy.

Update managers name in active directory?

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:
Expand|Select|Wrap|Line Numbers
  1. using (DirectoryEntry manager = new DirectoryEntry(GetDirectoryEntry()))
  2.                 using (DirectoryEntry minion = new DirectoryEntry(GetDirectoryEntry())) {
  3.                   minion.Properties["manager"].Value =
  4.                       manager.Properties["distinguishedName"].Value.ToString();
  5.                 }
Error: The value provided for adsObject does not implement IADs.

With this is one:
Expand|Select|Wrap|Line Numbers
  1. if (oEmployee.Manager != String.Empty) {
  2.                   dentry.Properties["manager"].Value = "CN=oEmployee.Manager,DC=domain, DC=net";
Error: System.DirectoryServices.DirectoryServicesCOMExcep tion (0x8007202F): A constraint violation occurred.(Exception from HRESULT: 0x8007202F)

And with this one:
Expand|Select|Wrap|Line Numbers
  1. if (oEmployee.Manager != String.Empty) {
  2.                   dentry.Properties["manager"].Value = oEmployee.Manager;
Error: System.DirectoryServices.DirectoryServicesCOMExcep tion (0x8007202F): A constraint violation occurred.(Exception from HRESULT: 0x8007202F)



This is my code:

Expand|Select|Wrap|Line Numbers
  1.     public void ModifyUsers(SortedList<string , clsEmployee> colEmployee) {
  2.       clsEmployee oEmployee;
  3.       DirectoryEntry dentry;
  4.       string empId;
  5.  
  6.       DirectoryEntry de = GetDirectoryEntry();
  7.       DirectorySearcher dirsearcher = new DirectorySearcher(de);
  8.       dirsearcher.PageSize = 1000;
  9.  
  10.       dirsearcher.Filter = "(&(objectCategory=person)(objectClass=user))";
  11.       SearchResultCollection dsCol = dirsearcher.FindAll();
  12.  
  13.       foreach (SearchResult restEnt in dsCol) {
  14.         dentry = restEnt.GetDirectoryEntry();
  15.  
  16.         if (dentry.Properties["employeeId"] != null) {
  17.           if (dentry.Properties["employeeId"].Value != null) {
  18.             empId = dentry.Properties["employeeId"].Value.ToString();
  19.             oEmployee = new clsEmployee();
  20.             try {
  21.               oEmployee = colEmployee[empId];
  22.             }
  23.             catch {
  24.               //do nothing
  25.             }
  26.             if (!oEmployee.empId.Equals(string.Empty)) {            
  27.  
  28.               //Update the information
  29.               try {
  30.                 if (oEmployee.Pager != String.Empty) {
  31.                   dentry.Properties["pager"].Value = oEmployee.Pager;
  32.                 }
  33.                 if (oEmployee.TelephoneNumber != String.Empty) {
  34.                   dentry.Properties["telephoneNumber"].Value = oEmployee.TelephoneNumber;
  35.                 }
  36.                 if (oEmployee.HomePhone != String.Empty) {
  37.                   dentry.Properties["homePhone"].Value = oEmployee.HomePhone;
  38.                 }
  39.                 if (oEmployee.Mobile != String.Empty) {
  40.                   dentry.Properties["mobile"].Value = oEmployee.Mobile;
  41.                 }
  42.                 if (oEmployee.Nextel != String.Empty) {
  43.                   dentry.Properties["ipphone"].Value = oEmployee.Nextel;
  44.                 }
  45.                 if (oEmployee.RadioId != String.Empty) {
  46.                   dentry.Properties["otherIpPhone"].Value = oEmployee.RadioId;
  47.                 }
  48.                 if (oEmployee.Department != String.Empty) {
  49.                   dentry.Properties["department"].Value = oEmployee.Department;
  50.                 }
  51.                 if (oEmployee.Manager != String.Empty) {
  52.                   dentry.Properties["manager"].Value = oEmployee.Manager;
  53.                 }
  54.                 using (DirectoryEntry entryManager = new DirectoryEntry(GetDirectoryEntry()))
  55.                 using (DirectoryEntry dirent = new DirectoryEntry(GetDirectoryEntry())) {
  56.                   dirent.Properties["manager"].Value =
  57.                       entryManager.Properties["manager"].Value = oEmployee.Manager;
  58.                 }
  59.                 //Update managers name
  60.                 //if (oEmployee.Manager != String.Empty) {
  61.                 //  dentry.Properties["distinguishedName"].Value = oEmployee.Manager;
  62.                 //}
  63.               }
  64.               catch (Exception e) {
  65.                 //For debugging
  66.                 Console.WriteLine(e.Message);
  67.               }
  68.               dentry.CommitChanges();
  69.               dentry.Close();
  70.             }
  71.           }
  72.         }
  73.       }
  74.  
Any ideas of how I can update the manager name?

Thanks.
Oct 16 '08 #1
2 2508
Curtis Rutland
3,256 Expert 2GB
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Oct 16 '08 #2
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Sorry, I forgot. I'll make sure to include them next time.

Thanks.
Oct 16 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Pam Ammond | last post: by
reply views Thread by Kooki | last post: by
reply views Thread by Kooki | last post: by
2 posts views Thread by Paul Hadfield | last post: by
reply views Thread by =?Utf-8?B?SmVhbmluZQ==?= | last post: by
3 posts views Thread by Michel Esber | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.