使用SPSecurity.RunWithElevatedPrivileges更新sharepoint用户配置文件属性

时间:2015-08-17 09:55:16

标签: sharepoint impersonation sharepoint-userprofile

我尝试使用下一个代码更改用户个人资料属性

SPSecurity.RunWithElevatedPrivileges(delegate(){
SPSite currentSite = new SPSite(SPContext.Current.Web.Url);

SPServiceContext serviceContext = SPServiceContext.GetContext(currentSite);
UserProfileManager upm = new UserProfileManager(serviceContext);
UserProfile up1 = upm.GetUserProfile("DOMAIN\\User3");
up1["CustomProperty"].Value=10;           
up1.Commit();

currentSite.Dispose();
});

当我用帐户User1打开页面时,它可以,它有权更改所有用户配置文件。但是,当我用User2打开页面(没有权限) - 我得到403错误。在调试器up1 [“CustomProperty”]中。值为null。

为什么SPSecurity.RunWithElevatedPrivileges无效,我该如何解决这个问题呢?

由于

1 个答案:

答案 0 :(得分:0)

我在下一篇文章中找到了对我的问题的描述 Impersonation does not work with UserProfileManager

作为一个原因,您可以在每次获取或设置用户配置文件属性时清除HttpContext。例如,下一个代码适用于我。

 SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            HttpContext tempCtx = HttpContext.Current;
            HttpContext.Current = null;

            UserProfile userProfile = GetUserProfile(user);
            userProfile["SomeProperty"].Value = points;
            userProfile.Commit();

            HttpContext.Current = tempCtx;
        });