管理SP2010用户配置文件作为用户配置文件管理员

时间:2011-08-10 21:30:23

标签: c# sharepoint sharepoint-2010

我有一个SharePoint 2010事件接收器,需要更新站点上另一个用户的用户配置文件,即不是触发该事件的用户。我尝试使用用户配置文件管理员的用户令牌打开UserProfileManager(如another question中所述),但由于某种原因,它仍然在触发事件的人的帐户下运行。
模拟User Profile Service应用程序管理员的正确方法是什么,以便我可以编辑任何用户的个人资料?

SPUserToken upmToken = web.AllUsers[@"domain\upadmin"].UserToken;//this user is a User Profile Service Application Administrator
using (SPSite upmSite = new SPSite(web.Url, upmToken))
{
    SPServiceContext context = SPServiceContext.GetContext(upmSite);
    UserProfileManager userProfileManager = new UserProfileManager(context, false);
    UserProfile userProfile = userProfileManager.GetUserProfile("anotheruser");
    userProfile["PictureUrl"].Value = pictureUrl;//System.UnauthorizedAccessException: Attempted to perform an unauthorized operation
    userProfile.Commit();
}

如果我将自己添加到User Profile Service应用程序管理员,代码运行正常,因此显然仍在尝试在触发事件接收器的用户帐户下打开UserProfileManager。 我认为将应用程序池帐户添加到用户配置文件管理员以便使用RunWithElevatedPermissions是不可接受的。

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用系统帐户令牌(SPUserToken.SystemAccount)? 使用(SPSite mySiteSite = new SPSite(mySiteHostUrl,SPUserToken.SystemAccount))