Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1389
  • Last Modified:

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

0
realcoding
Asked:
realcoding
  • 6
  • 5
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
do u mean all users who shares the same "manager" attribute in the AD?
0
 
realcodingAuthor Commented:
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.
0
 
Meir RivkinFull stack Software EngineerCommented:
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
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
realcodingAuthor Commented:
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.

0
 
realcodingAuthor Commented:
and the rub is i need a .net solution to this
0
 
Meir RivkinFull stack Software EngineerCommented:
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

0
 
Meir RivkinFull stack Software EngineerCommented:
you need to add referenece to System.Linq;
0
 
realcodingAuthor Commented:
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";

0
 
realcodingAuthor Commented:
also im getting a problem with the line

if (manager.Contains(user))

user does not exist in the current context?
0
 
realcodingAuthor Commented:
finally GetDirectReportsInternal has no return value ...
0
 
Meir RivkinFull stack Software EngineerCommented:
>>what value do i use for "ad_server" all i have been using is amrs as per my original code sample.

set LDAP_ROOT_PATH  as "LDAP://DC=amrs,DC=win,DC=ml,dc=COM"

>>also im getting a problem with the line
>>if (manager.Contains(user))

user is being passed as argument to the function FindUsersByManager

>>finally GetDirectReportsInternal has no return value ...
i never posted a function called GetDirectReportsInternal nor did you.

i think you messed up with the code...
try rearrange your code and use mine with only modifying what necessary (LDAP_ROOT_PATH  and cn_user)
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now