Solved

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

Posted on 2010-11-30
11
1,231 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
  • 6
  • 5
11 Comments
 
LVL 42

Expert Comment

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

Author Comment

by:realcoding
Comment Utility
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
Comment Utility
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
 

Author Comment

by:realcoding
Comment Utility
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
Comment Utility
and the rub is i need a .net solution to this
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
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
Comment Utility
you need to add referenece to System.Linq;
0
 

Author Comment

by:realcoding
Comment Utility
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
Comment Utility
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
Comment Utility
finally GetDirectReportsInternal has no return value ...
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
Comment Utility
>>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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

[b]Ok so now I will show you how to add a user name to the description at login. [/b] First connect to your DC (Domain Controller / Active Directory Server) SET PERMISSIONS FOR SCRIPT TO UPDATE COMPUTER DESCRIPTION TO USERNAME 1. Open Active …
In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
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 configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now