Sharepoint,读取用户名以查询Active Directory和筛选器列表

时间:2013-11-25 13:07:07

标签: c# sharepoint-2010 active-directory

我的Sql-Table中有一个EmployeeID列,它也与从“外部内容类型”在SharePoint Designer中创建的SharePoint列表同步。我们公司的Active Directory帐户也有一个EmployeeID字段。在页面加载时,如何读取用户登录名,查询Ac​​tive Directory以获取EmployeeID,并使用SharePoint Foundation中检索到的EmployeID筛选列表。

P.S。我能够使用Silverlight Webpart和WCF Servie执行此操作,但我需要在本机SharePoint中执行此操作。 

1 个答案:

答案 0 :(得分:0)

这是我用来查询AD的代码:

public class ADHelper
{
        public static Employee getUserName(string loginName)
        {
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
            UserPrincipal principal = new UserPrincipal(ctx);
            principal.SamAccountName = Regex.Replace(loginName, ".*\\\\(.*)", "$1", RegexOptions.None);

            // create your principal searcher passing in the QBE principal    
            PrincipalSearcher srch = new PrincipalSearcher(principal);

            // find all matches
            Employee employee = new Employee();
            foreach (var found in srch.FindAll())
            {
                UserPrincipal principal2 = (UserPrincipal)found;
                //FirstName
                employee.FirstName = principal2.GivenName;
                //Middle Name
                employee.MiddleName = principal2.MiddleName;
                //LastName
                employee.LastName = principal2.Surname;
            }
            return employee;
        }
    }

    public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string MiddleName { get; set; }
        public string getFullName()
        {
            return LastName + ", " + FirstName + " " + MiddleName;
        }       
    }

这就是我从SharePoint中调用它的方式:

ADHelper.getUserName(SPContext.Current.Web.CurrentUser.LoginName);