I would like to speed up the loading time for an asp.net project I have been working on. One of the dropdownlists is a list of managers that is obtained by comparing a list of employees from a sql table to Active Directory membership.
This is called from Page_Load
SqlDataSource empDB = (SqlDataSource)Page.Master.FindControl("EmployeeData");
DataView dv = (DataView)empDB.Select(DataSourceSelectArguments.Empty);
foreach (DataRowView drv in dv)
{
employeeList.Add(new Employee(int.Parse(drv["EMP_ID"].ToString()), drv ["FULL_NAME"].ToString()));
}
managerTest();
managerDropDownList.DataSource = managerList;
managerDropDownList.DataTextField = "fullName";
managerDropDownList.DataValueField = "emp_Id";
managerDropDownList.DataBind();
The manager test method:
private void managerTest()
{
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "the_domain");
GroupPrincipal managersGroup = GroupPrincipal.FindByIdentity(domainContext, "Managers and Supervisors");
GroupPrincipal offManagersGroup = GroupPrincipal.FindByIdentity(domainContext, "Office Managers");
GroupPrincipal executivesGroup = GroupPrincipal.FindByIdentity(domainContext, "Executives");
GroupPrincipal managers2Group = GroupPrincipal.FindByIdentity(domainContext, "Managers");
foreach (Employee emp in employeeList)
{
UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, emp.fullName);
if (user != null)
{
if (user.IsMemberOf(managersGroup) || user.IsMemberOf(offManagersGroup) || user.IsMemberOf(executivesGroup) || user.IsMemberOf(managers2Group))
{
managerList.Add(emp);
}
}
}
}
Employee is just a simple class that separates the values for first name, last name and full name.
The issue is that the page loads really slow and there are several controls on the page that postback and causes the postback to take a long time. Is there anything I can do to make this work more efficiently?
A couple things. First. You should wrap all code you posted in page_load in a
if (!IsPostBack). You only need to bind that one time, not every time the page loads. Unless you’ve turned off viewstate of course.Second. How often do managers change? Probably not that often. I would recommend storing this in the asp.net
Cache. Heck, you might want to do cache it inGlobal.asaxApplication_Start. Otherwise, the first person to visit the page will get a performance hit. But that’s certainly much better then loading it EVERY TIME someone visits the page. And then you CAN turn off viewstate on the page if you want.Next. I think the way you are using AD is very inefficient. You are calling it for EVERY employee in a loop. And the only reason why you are doing that is to find out if they are a manager. Instead of doing it that way, find out who all the managers are first. In other words: call AD to get the members of the groups you care about. See this question: get all users from a group in Active Directory for how you can find all the members of an AD group. I suspect that would REALLY help with performance.