//Pass a group name as a parameter and returns user information public List <Employee> GetUserPerByAdGroup(string gName) { var userList = new List <Employee>(); sDomain = this.DomanName; using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, sDomain)) { GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, gName); if (group != null) { // iterate over the group's members foreach (Principal p in group.GetMembers()) { //Initializing an object which will be called mostly to populate user dropdown lists var user = UserPrincipal.FindByIdentity(ctx, p.SamAccountName); var employeeObject = new Employee(); try { employeeObject.EmployeeFirstName = string.IsNullOrWhiteSpace(user.GivenName) ? user.Name : user.GivenName; employeeObject.EmployeeSurname = user.Surname; employeeObject.EmployeeNumber = user.VoiceTelephoneNumber; employeeObject.EmailAddress = user.EmailAddress; employeeObject.UserName = p.SamAccountName; employeeObject.DisplayName = user.GivenName + " " + user.Surname; //Adding an object to a class if (!string.IsNullOrWhiteSpace(employeeObject.DisplayName)) { userList.Add(employeeObject); } } catch (Exception) { } } } } //There are instances (for example raters) where a group becomes a user hence a group list is appended into a user information var ADGroupList = new ADUsers().GetAdGroupsForUser(Generic.GetCurrentLogonUserName(), new ADUsers().DomanName); for (int i = 0; i < ADGroupList.Count; i++) { userList.Add(new Employee() { DisplayName = ADGroupList[i] }); } return(userList.OrderBy(c => c.DisplayName).ToList()); }
//This method returns employee name and surname from active Directory public static List <Rater> GetEmployeeInformation() { var empList = new List <Employee>(); var raterObject = new List <Rater>(); var list = new ADUsers().GetAdGroupsForUser(Generic.GetCurrentLogonUserName(), new ADUsers().DomanName); var raterList = new List <Rater>(); for (int i = 0; i < list.Count; i++) { //Pass DG-Futuregrowth as a group name and only returns all employees that have access to that group var empGroupList = new ADUsers().GetUserPerByAdGroup("DG-Futuregrowth"); for (int j = 0; j < empGroupList.Count; j++) { raterList.Add(new Rater() { Name = empGroupList[j].EmployeeFirstName + " " + empGroupList[j].EmployeeSurname }); } break; } return(raterList); }