检索和编辑Sharepoint Active Directory用户配置文件属性

时间:2014-04-08 15:33:21

标签: c# asp.net-mvc-4 sharepoint web-applications active-directory

我的Web应用程序是使用HTML5和Jquery的ASP.NET MVC 4 Web应用程序。

我正在编写Web应用程序,以使用Active Directory从Sharepoint服务器检索和编辑数据。

我能够很好地检索信息,但我试图找到一种方法来编辑和提交对活动目录帐户的更改。我无法在任何与远程Web应用程序访问相关的代码示例中找到代码示例。我见过的唯一编辑样本只能在SharePoint服务器上完成。

我想知道我们是否完全错过了有关Active Directory的内容,以及我尝试做的事情是否可行。

注:

我还没有能够找到用于编辑Active Directory信息的代码。这是我到目前为止检索的代码。我希望能够回退信息,编辑名字或姓氏等属性,然后将更改提交到SharePoint Active Directory。

提前感谢您的回答!

ClientContext currentContext = new ClientContext(serverAddress);
        currentContext.Credentials = new System.Net.NetworkCredential(adminAccount, password);

        const string targetUser = "domain\\targetAccountName";

        Microsoft.SharePoint.Client.UserProfiles.PeopleManager peopleManager = new Microsoft.SharePoint.Client.UserProfiles.PeopleManager(currentContext);
        Microsoft.SharePoint.Client.UserProfiles.PersonProperties personProperties = peopleManager.GetPropertiesFor(targetUser);

        currentContext.Load(personProperties, p => p.AccountName, p => p.UserProfileProperties);
        currentContext.ExecuteQuery();

        foreach (var property in personProperties.UserProfileProperties)
        {
            //Pull User Account Name
            //Edit Account name to new value
            //Commit changes
        }            

1 个答案:

答案 0 :(得分:0)

看起来PersonProperties类仅提供只读访问权限,因为所有属性仅显示Get。 MSDN PersonProperties

如果您希望保留在SharePoint中,看起来您需要查看UserProfile class。该页面有一个很好的例子,可以检索一个帐户,然后设置一些属性。

如果您不需要特定于SharePoint的属性并希望使用易于使用的格式,则可以检索UserPrincipal。它可以让您轻松访问常见的用户属性。

using (var context = new PrincipalContext(ContextType.Domain, "domainServer", 
                            "DC=domain,DC=local", adminAccount, password))
{
    var userPrincipal = UserPrincipal.FindByIdentity(context, 
                            IdentityType.SamAccountName, "targetAccountName");
    if (userPrincipal != null)
    {
        userPrincipal.GivenName = "NewFirstName";
        // etc, etc.
    }
}
相关问题