Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-11-30
11
Medium Priority
?
1,352 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
Suggested Courses

722 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