I know that Active Directory allows for at least one object to have an assigned manager (attribute) that is the same as the current object. In other words, the employee is their own manager (i.e., CEO).
Does anyone know if it is possible to have more than one object where the manager is the same as the given object (i.e., there are co-CEO's)? Microsoft is silent on this subject (see link below).
I am extracting AD objects to a SQL server table and developing a recursive query to build the employee-to-manager hierarchy. The first step of the query gets the CEO where the DistinguishedName = ManagerDistinguishedName
. The second step of the query gets all employees where the DistinguishedName <> ManagerDistinguishedName
.
The query currently works where there is just one CEO for the firm. Unfortunately, I do not have a development environment for AD to test for co-CEO's and how it might impact the query. In other words, I am not sure if there is the potential for an infinite loop based on the data and query constraints.
UPDATE
;WITH EmployeeList AS (
SELECT EmployeeObjectKey = ObjectKey, EmployeeFullName = FullName, EmployeeSamAccountName = SamAccountName, ManagerObjectKey
,ManagerFullName = FullName, ManagerSamAccountName = SamAccountName, EmployeeLevel = 1
FROM #TEMP_ManagerToEmployeeHierarchy
WHERE ObjectKey = ManagerObjectKey -- The CEO of the organization should be the only employee who's manager is themself.
UNION ALL
SELECT EmployeeObjectKey = E.ObjectKey, EmployeeFullName = E.FullName, EmployeeSamAccountName = E.SamAccountName, E.ManagerObjectKey
,ManagerFullName = M.EmployeeFullName, ManagerSamAccountName = M.EmployeeSamAccountName, EmployeeLevel = M.EmployeeLevel + 1
FROM #TEMP_ManagerToEmployeeHierarchy E -- Employee
INNER JOIN EmployeeList M -- Manager
ON E.ManagerObjectKey = M.EmployeeObjectKey
AND ObjectKey <> E.ManagerObjectKey -- Do not remove this exclusion, otherwise an infinite loop is created and the query will fail.
)
SELECT *
FROM EmployeeList
ORDER BY EmployeeLevel, ManagerFullName, EmployeeFullName
Where:
- .#TEMP_ManagerToEmployeeHierarchy is a temp table that only contains active user accounts.
- ObjectKey is an IDENTITY value based on the order that objects are inserted. In other words, it is a translation of the objectGuid attribute.