Update sharepoint user profile property with SPSecurity.RunWithElevatedPrivileges

689 Views Asked by At

I try to change user profile property with next code

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();
});

And it's all right when i open page with account User1, which have permissions to change all user profiles. But when i open page with User2(with no permissions) - i get 403 error. In debugger up1["CustomProperty"].Value is null.

Why SPSecurity.RunWithElevatedPrivileges have no effect and how can i solve this problem?

Thanks

1

There are 1 best solutions below

0
On

I found description of my problem in next article Impersonation does not work with UserProfileManager

As a reason you can clear HttpContext each time you get or set user profile properties. For example next code works fin for me.

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

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

            HttpContext.Current = tempCtx;
        });