Link to home
Start Free TrialLog in
Avatar of realcoding
realcodingFlag for United States of America

asked on

c# using ActiveDir to get names under a managers org chart?

i am using ODBC to pull data from active directory to get the email for a particular user name using the code below.

how can i use AD to pull all the user names of people who report into a particular manager?

i can transverse the org chart in outlook so im thinking i can do the same using AD...

ideas?


 
System.Data.OleDb.OleDbConnection con;
        System.Data.OleDb.OleDbCommand cmd;

        con = new System.Data.OleDb.OleDbConnection("Provider=ADsDSOObject;dsn=Active Directory Provider");
        con.Open();

        //Create a command object on this connection
        string strSQL = "SELECT mail FROM 'LDAP://DC=amrs,DC=win,DC=ml,dc=COM' WHERE samaccountname = '" + UserName.Replace(@"AMRS\", "") + "'";
        cmd = new System.Data.OleDb.OleDbCommand(strSQL, con);

        try
        {
            return Convert.ToString (  cmd.ExecuteScalar() );
        }
        catch (System.Data.OleDb.OleDbException exc)
        {
            return "ERROR: " + exc.ToString();
        }
        finally
        {
            con.Close();
        }

Open in new window

Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

do u mean all users who shares the same "manager" attribute in the AD?
Avatar of realcoding

ASKER

i want to enter a username of a manager who can have 3 direct reports and those three can have 5,6, and 7 reporting into them... and so on down the org cart.

sorry if i was unclear.
sorry, i don't get it.
which procedure you wish to perform on the AD?
you said getting all users which (in the org chart) report to a required manager.
the manager name can be retrieved via "manager" attribute of the user in AD?
Untitled.jpg
manager is being used. so i want to query AD to get all user names that have 'mike' as manager, then for each of those to get all the users who have those guys as manager and so on...

this will result in a list of users who report to people who report to people who report to some manager in the log run.

in other words it will give me a list of people who work under a certain manager directly and indirectly.

and the rub is i need a .net solution to this
change the LDAP_ROOT_PATH.
the cn_user should be the common name of the user as appears in AD. (attribute "cn").
_users will contain all direct and indirect users under cn_user.

class ADUserInfo
    {
        public string Manager { get; set; }
        public string User { get; set; }
    }

    static class Program
    {
        static readonly string LDAP_ROOT_PATH = "LDAP://ad_server/cn=users,dc=soap,dc=com";
        static List<string> _users = new List<string>();

        static void Main()
        {
            string cn_user = "Jonny Smith";
            GetUsersByManager(cn_user);
        }

        private static void GetUsersByManager(string user)
        {
            DirectoryEntry objDirEnt = new DirectoryEntry(LDAP_ROOT_PATH);
            DirectorySearcher mySearcher = new DirectorySearcher(objDirEnt);

            mySearcher.PageSize = 10000;
            mySearcher.Filter = "(&(objectCategory=user))";
            mySearcher.PropertiesToLoad.Add("cn");
            mySearcher.PropertiesToLoad.Add("Manager");
            mySearcher.SearchScope = SearchScope.Subtree;

            SearchResultCollection resultCol = mySearcher.FindAll();
            ADUserInfo[] col = resultCol.Cast<SearchResult>().Select(n => new ADUserInfo { Manager = n.Properties["Manager"].Count > 0 ? n.Properties["Manager"][0].ToString() : null, User = n.Properties["cn"][0].ToString() }).ToArray();
            foreach (SearchResult result in resultCol)
            {
                if (result.Properties["Manager"].Count > 0)
                {
                    foreach (var item in result.Properties["Manager"])
                    {
                        string manager = item.ToString();
                        if (manager.Contains(user))
                        {
                            FindUsersByManager(col, result.Properties["cn"][0].ToString());
                        }
                    }
                }
            }
        }

        private static void FindUsersByManager(ADUserInfo[] col, string user)
        {
            _users.Add(user);
            foreach (ADUserInfo result in col)
            {
                if (result.Manager != null)
                {
                    string cn = result.User;

                    string manager = result.Manager;
                    if (manager.Contains(user))
                    {
                        FindUsersByManager(col, cn);
                    }
                }
            }
        }

Open in new window

you need to add referenece to System.Linq;
what value do i use for "ad_server" all i have been using is amrs as per my original code sample.
do i then use ?

static readonly string LDAP_ROOT_PATH = "LDAP://amrs/cn=users,dc=smrs,dc=ml,dc=com";

also im getting a problem with the line

if (manager.Contains(user))

user does not exist in the current context?
finally GetDirectReportsInternal has no return value ...
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial