Esempio n. 1
0
        //取得Store Operations部门,及user profile中员工部门不在Department list的所有员工
        private void GetAllEmployeeByStoreOperationsDept()
        {
            System.Text.StringBuilder strDept = new System.Text.StringBuilder();

            //Department  List Items
            SPList listDepartment = SharePointUtil.GetList(SPContext.Current.Site.RootWeb, CAConstants.ListName.Department);
            foreach (SPListItem item in listDepartment.Items)
            {
                if (item["DisplayName"] == null)
                    continue;
                string strTempSPDept = item["Name"].ToString();
                if (!strTempSPDept.Equals(strDeptName, StringComparison.CurrentCultureIgnoreCase))
                {
                    strDept.Append(strTempSPDept + ";");
                }
            }

            Employee employee = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    Microsoft.Office.Server.ServerContext context = Microsoft.Office.Server.ServerContext.GetContext(site);
                    Microsoft.Office.Server.UserProfiles.UserProfileManager profileManager = new Microsoft.Office.Server.UserProfiles.UserProfileManager(context);
                    foreach (Microsoft.Office.Server.UserProfiles.UserProfile profile in profileManager)
                    {
                        if (profile[Microsoft.Office.Server.UserProfiles.PropertyConstants.Department].Value == null)
                            continue;

                        if (!strDept.ToString().Contains(profile[Microsoft.Office.Server.UserProfiles.PropertyConstants.Department].Value.ToString()))
                        {
                            employee = UserProfileUtil.InstanceEmployee(profile, site);
                            if (null != employee)
                            {
                                heads.Add(employee);
                            }
                        }
                    }
                }
            }
            );

            heads.Sort(delegate(Employee emp1, Employee emp2)
            {
                if (emp1.Department != emp2.Department)
                {
                    return emp1.Department.CompareTo(emp2.Department);
                }

                var jobIdx1 = emp1.JobLevel.IndexOf("-");
                var jobIdx2 = emp2.JobLevel.IndexOf("-");
                if (jobIdx1 == -1)
                {
                    if (jobIdx2 == -1)
                        return 0;
                    else
                        return 1;
                }
                else
                {
                    if (jobIdx2 == -1)
                        return -1;
                }

                var start = jobIdx1 + 1; //L-3
                var job1 = int.Parse(emp1.JobLevel.Substring(start));
                var job2 = int.Parse(emp2.JobLevel.Substring(start));

                if (emp1.JobLevel.Equals(emp2.JobLevel, StringComparison.CurrentCultureIgnoreCase))
                    return emp1.DisplayName.CompareTo(emp2.DisplayName);
                else
                    return job1.CompareTo(job2);

            });
        }
Esempio n. 2
0
        //取得Store Operations部门,及user profile中员工部门不在Department list的所有员工
        private void GetAllEmployeeByStoreOperationsDept()
        {
            trvEmployees.Nodes.Clear();
            employees.Clear();

            //string manager = UserProfileUtil.GetDepartmentManager(strDept);

            string strSPDept = Page.Request["Dept"];

            List<string> manager = GetDepartmentManager(strSPDept);

            System.Text.StringBuilder strDepartment = new System.Text.StringBuilder();

            //Department  List Items
            SPList listDepartment = SharePointUtil.GetList(SPContext.Current.Site.RootWeb, CAConstants.ListName.Department);
            foreach (SPListItem item in listDepartment.Items)
            {
                if (item["DisplayName"] == null)
                    continue;
                string strTempSPDept = item["Name"].ToString();
                if (!strTempSPDept.Equals(strDept, StringComparison.CurrentCultureIgnoreCase))
                {
                    strDepartment.Append(strTempSPDept + ";");
                }
            }

            Employee empl = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    Microsoft.Office.Server.ServerContext context = Microsoft.Office.Server.ServerContext.GetContext(site);
                    Microsoft.Office.Server.UserProfiles.UserProfileManager profileManager = new Microsoft.Office.Server.UserProfiles.UserProfileManager(context);
                    foreach (Microsoft.Office.Server.UserProfiles.UserProfile profile in profileManager)
                    {
                        if (profile[Microsoft.Office.Server.UserProfiles.PropertyConstants.Department].Value == null)
                            continue;

                        if (!strDepartment.ToString().Contains(profile[Microsoft.Office.Server.UserProfiles.PropertyConstants.Department].Value.ToString()))
                        {
                            empl = UserProfileUtil.InstanceEmployee(profile, site);
                            if (null != empl)
                            {
                                employees.Add(empl);
                            }
                        }
                    }
                }
            }
            );

            employees.Sort(delegate(Employee emp1, Employee emp2)
            {
                //if (emp1.Department != emp2.Department)
                //{
                //    return emp1.Department.CompareTo(emp2.Department);
                //}

                var jobIdx1 = emp1.JobLevel.IndexOf("-");
                var jobIdx2 = emp2.JobLevel.IndexOf("-");
                if (jobIdx1 == -1)
                {
                    if (jobIdx2 == -1)
                        return 0;
                    else
                        return 1;
                }
                else
                {
                    if (jobIdx2 == -1)
                        return -1;
                }

                var start = jobIdx1 + 1; //L-3
                var job1 = int.Parse(emp1.JobLevel.Substring(start));
                var job2 = int.Parse(emp2.JobLevel.Substring(start));

                if (emp1.JobLevel.Equals(emp2.JobLevel, StringComparison.CurrentCultureIgnoreCase))
                    return emp1.DisplayName.CompareTo(emp2.DisplayName);
                else
                    return job1.CompareTo(job2);

            });

            Dictionary<string, TreeNode> processed = new Dictionary<string, TreeNode>();
            TreeNode tempRoot = new TreeNode("All Employees", "All Employees");

            foreach (Employee employee in employees)
            {
                TreeNode newNode = new TreeNode();
                newNode.Value = employee.DisplayName;

                //if (manager.Equals(employee.UserAccount, StringComparison.CurrentCultureIgnoreCase))
                if (manager.Contains(employee.UserAccount.ToLower()))
                {
                    newNode.Text = string.Format("<span style=\"font-weight:bold\" onclick=\"return SelectUser('{0}','{1}')\">{0}</span>", employee.DisplayName, strDept);
                }
                else
                {
                    newNode.Text = string.Format("<span onclick=\"return SelectUser('{0}','{1}')\">{0}</span>", employee.DisplayName, strDept);
                }
                    tempRoot.ChildNodes.Add(newNode);
            }

            trvEmployees.Nodes.Add(tempRoot);

            //foreach (Employee employee in employees)
            //{
            //    if (processed.Keys.Contains(employee.DisplayName))
            //        continue;

            //    Employee tmpEmployee = employee;
            //    TreeNode managerNode = null;
            //    while (tmpEmployee != null)
            //    {
            //        if (processed.Keys.Contains(tmpEmployee.DisplayName))
            //        {
            //            tmpEmployee = null;
            //            continue;
            //        }
            //        else if (string.IsNullOrEmpty(tmpEmployee.Manager) || (tmpEmployee.UserAccount.Equals(tmpEmployee.Manager)))
            //        {
            //            //TreeNode newNode = new TreeNode(tmpEmployee.DisplayName, tmpEmployee.DisplayName);
            //            TreeNode newNode = new TreeNode();
            //            newNode.Value = tmpEmployee.DisplayName;
            //            newNode.Text = string.Format("<span onclick=\"return SelectUser('{0}','{1}')\">{0}</span>", tmpEmployee.DisplayName, strDept);
            //            if (managerNode != null)
            //            {
            //                newNode.ChildNodes.Add(managerNode);
            //            }
            //            managerNode = null;
            //            tempRoot.ChildNodes.Add(newNode);
            //            processed.Add(tmpEmployee.DisplayName, newNode);
            //            tmpEmployee = null;
            //            continue;
            //        }

            //        //TreeNode node = new TreeNode(tmpEmployee.DisplayName, tmpEmployee.DisplayName);
            //        TreeNode node = new TreeNode();
            //        node.Value = tmpEmployee.DisplayName;
            //        node.Text = string.Format("<span onclick=\"return SelectUser('{0}','{1}')\">{0}</span>", tmpEmployee.DisplayName, strDept);
            //        if (managerNode != null)
            //        {
            //            node.ChildNodes.Add(managerNode);
            //        }
            //        managerNode = node;
            //        processed.Add(tmpEmployee.DisplayName, managerNode);

            //        tmpEmployee = employees.Find(new Predicate<Employee>(delegate(Employee emp)
            //        {
            //            return emp.UserAccount == tmpEmployee.Manager;
            //        }));

            //        if (tmpEmployee == null)
            //        {
            //            tempRoot.ChildNodes.Add(managerNode);
            //        }
            //        else if (processed.TryGetValue(tmpEmployee.DisplayName, out node))
            //        {
            //            node.ChildNodes.Add(managerNode);
            //            tmpEmployee = null;
            //            continue;
            //        }
            //    }
            //}
            //trvEmployees.Nodes.Add(tempRoot);
        }