Click here to Skip to main content
Click here to Skip to main content

Update Manager Name in Active Directory Using C#

By , 10 Nov 2008
 

Introduction

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.

Background

Most user fields in Active Directory can be inserted as normal text, but Manager has a distinguished name syntax attribute (2.5.5.1), 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", 
                           @"alfanar.com\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.PropertiesToLoad.Add("samAccountName");
mySearcher.PropertiesToLoad.Add("Manager");
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.PropertiesToLoad.Add("DistinguishedName");
        managerSearcher.SearchScope = SearchScope.Subtree;

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


        if (managerResultCol != null)
        {
            managerResult = managerResultCol[0];
            string managerName = 
              (string)managerResult.Properties["DistinguishedName"][0];

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

License

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionTnxmembersepel24-Nov-11 22:57 
Thank you so much man!
I really have so problem to update this field!
sepel

GeneralGood Job thank u ,here is the VB CodememberSamsooom29-Mar-10 11:35 
You are the man, your code just awesome
for the vb users ( like me ) here is your code in vb2005 language( all the credit to the author )
 
      
Public Shared Function GetDirectoryEntry() As DirectoryEntry
            Dim dirEntry As DirectoryEntry = New DirectoryEntry()
            dirEntry.Path = "LDAP://DC=??????,DC=???"
            dirEntry.Username = ???????
            dirEntry.Password = ???????
            Return dirEntry
End Function
Public Shared Function UpdateManager(ByVal UserAccount As String, ByVal ManagerAccount As String)
            Dim EmployeeSAM As String = UserAccount
 
            'you have to use the current user account or any domain admin user account
            Dim objDirEnt As DirectoryEntry = GetDirectoryEntry()
 
            'searcher object to select the current user from the Active directory
            Dim mySearcher As DirectorySearcher = 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.PropertiesToLoad.Add("samAccountName")
            mySearcher.PropertiesToLoad.Add("Manager")
            mySearcher.SearchScope = SearchScope.Subtree
            Dim result As SearchResult
            Dim resultCol As SearchResultCollection = mySearcher.FindAll()
            'you can use the colliction for bulk update
            If resultCol.Count <> 0 Then
                For counter As Integer = 0 To resultCol.Count Step 2
                    result = resultCol(counter)
                    Dim Userde As DirectoryEntry = result.GetDirectoryEntry()
                    'the directory entry for the user 
                    If (result.Properties.Contains("samaccountname")) Then
                        'check if it is a valid entry
                        Dim ManagerSAM As String = ManagerAccount 'manager Account 
                        Dim managerSearcher As DirectorySearcher = New DirectorySearcher(objDirEnt)
                        managerSearcher.PageSize = 10
                        managerSearcher.Filter = "(&(objectCategory=user)(samAccountName=" & ManagerSAM & "))"
                        managerSearcher.PropertiesToLoad.Add("DistinguishedName")
                        managerSearcher.SearchScope = SearchScope.Subtree
                        Dim managerResult As SearchResult
                        Dim managerResultCol As SearchResultCollection = managerSearcher.FindAll()
                        If managerResultCol.Count <> 0 Then
                            managerResult = managerResultCol(0)
                            Dim managerName As String = managerResult.Properties("DistinguishedName")(0)
                            'here update the manager name 
                            'in the user directtory entry "Userde"
                            Userde.Properties("Manager").Value = managerName
                            Userde.CommitChanges()
                        End If
                    End If
                Next
            End If
        End Function

GeneralRe: Good Job thank u ,here is the VB Codemembergarry.aguirre17-Apr-12 15:34 
Nice work. Got one question thought, what is Userde in Userde.CommitChanges?

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

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