【问题标题】:Sharepoint, Reading Username to Query Active Directory and Filter ListSharepoint,读取用户名以查询 Active Directory 和过滤列表
【发布时间】:2013-12-10 05:28:18
【问题描述】:
我的 Sql-Table 中有一个 EmployeeID 列,该列也与 SharePoint Designer 中从“外部内容类型”创建的 SharePoint 列表同步。我们公司的 Active Directory 帐户也有一个 EmployeeID 字段。当页面加载时,我如何读取用户登录名、在 Active Directory 中查询 EmployeeID 并使用 SharePoint Foundation 中检索到的 EmployeID 过滤列表。
附:我可以使用 Silverlight Webpart 和 WCF 服务来做到这一点,但我需要在本机 SharePoint 中做到这一点。
【问题讨论】:
标签:
c#
sharepoint-2010
active-directory
【解决方案1】:
这是我用来查询 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);