Click here to Skip to main content
13,044,551 members (60,763 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 10 Nov 2008

Update Manager Name in Active Directory Using C#

, 10 Nov 2008
Rate this:
Please Sign up or sign in to vote.
How to update manager name in Active Directory using C#.


I was trying to update the manager name in Active Directory, but I faced some problems while doing that. I searched the Internet, but didn't find anything useful about this topic.


Most user fields in Active Directory can be inserted as normal text, but Manager has a distinguished name syntax attribute (, so you have to supply a valid DN for it, not just any string.

Using the code

First, you have to use the System.DirectoryServices and System.DirectoryServices.ActiveDirectory namespaces.

using System;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;

This code does the actual work:

string EmployeeSAM = "tamer.tharwat";

//you have to use the current user account or any domain admin user account
DirectoryEntry objDirEnt = new DirectoryEntry("LDAP://AlfanarCo", 
                           @"\Tamer.Tharwat", "****");

//searcher object to select the current user from the Active directory
DirectorySearcher mySearcher = new DirectorySearcher(objDirEnt);

mySearcher.PageSize = 10000;
//you can increase it according to your active directory database size

mySearcher.Filter = "(&(objectCategory=user)(samAccountName="+EmployeeSAM+"))";
//you can add any filter you want 

mySearcher.SearchScope = SearchScope.Subtree;

SearchResult result;
SearchResultCollection resultCol = mySearcher.FindAll();
//you can use the colliction for bulk update

if (resultCol != null)

for (int counter = 0; counter < resultCol.Count; counter++)
    result = resultCol[counter];

    DirectoryEntry Userde = result.GetDirectoryEntry();
    //the directory entry for the user 

    if (result.Properties.Contains("samaccountname"))
    //check if it is a valid entry

        string ManagerSAM = "aymanb";//manager Account 
        DirectorySearcher managerSearcher = new DirectorySearcher(objDirEnt);
        managerSearcher.PageSize = 10;
        managerSearcher.Filter = "(&(objectCategory=user)(samAccountName=" + 
                                 ManagerSAM + "))";
        managerSearcher.SearchScope = SearchScope.Subtree;

        SearchResult managerResult;
        SearchResultCollection managerResultCol = managerSearcher.FindAll();

        if (managerResultCol != null)
            managerResult = managerResultCol[0];
            string managerName = 

            //here update the manager name 
            //in the user directtory entry "Userde"
            (Userde.Properties["Manager"]).Value = managerName;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tamer Yousry Tharwat
Software Developer (Senior) Alfanar IT
Egypt Egypt
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionTnx Pin
sepel24-Nov-11 22:57
membersepel24-Nov-11 22:57 
GeneralGood Job thank u ,here is the VB Code Pin
Samsooom29-Mar-10 11:35
memberSamsooom29-Mar-10 11:35 
GeneralRe: Good Job thank u ,here is the VB Code Pin
garry.aguirre17-Apr-12 15:34
membergarry.aguirre17-Apr-12 15:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 11 Nov 2008
Article Copyright 2008 by Tamer Yousry Tharwat
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid