Solved

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

Posted on 2010-11-30
11
1,322 Views
Last Modified: 2012-05-10
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
Comment
Question by:realcoding
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 34239550
do u mean all users who shares the same "manager" attribute in the AD?
0
 

Author Comment

by:realcoding
ID: 34239614
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
 
LVL 42

Expert Comment

by:sedgwick
ID: 34240091
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:realcoding
ID: 34240246
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
 

Author Comment

by:realcoding
ID: 34240250
and the rub is i need a .net solution to this
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 34247578
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
 
LVL 42

Expert Comment

by:sedgwick
ID: 34247584
you need to add referenece to System.Linq;
0
 

Author Comment

by:realcoding
ID: 34248569
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
 

Author Comment

by:realcoding
ID: 34248611
also im getting a problem with the line

if (manager.Contains(user))

user does not exist in the current context?
0
 

Author Comment

by:realcoding
ID: 34248737
finally GetDirectReportsInternal has no return value ...
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 34255240
>>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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Let's recap what we learned from yesterday's Skyport Systems webinar.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question