Search code examples
c#sharepoint-2010active-directory

Update user profile throws PropertyNotEditableException in sharepoint 2010


I am trying to update a sharepoint 2010 users profile and I keep getting this error:

Microsoft.Office.Server.UserProfiles.PropertyNotEditableException: Property Not
Editable: This property can only be modified by an administrator. 
at Microsoft.Office.Server.UserProfiles.UserProfileValueCollection.
CheckUpdatePermissions()

I'm updating AD first with a separate block of code (which works). We are using the profile sync service so the value does eventually propagate down but we'd like to update the SP profile at the same time to show the change immediately.

code:

using (System.Web.Hosting.HostingEnvironment.Impersonate())
{
  SPSecurity.RunWithElevatedPrivileges(delegate()
  {
    Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
    using (var site = new SPSite(SPContext.Current.Site.ID))
    {
        try
        {
            SPServiceContext sc = SPServiceContext.GetContext(site);
            UserProfileManager userProfileMangager = new UserProfileManager(sc);
            SPUser user = site.RootWeb.EnsureUser(loginName);
            UserProfile profile = userProfileMangager.GetUserProfile(loginName);
                try
                {
                    profile["WorkEmail"].Value = tbEmail.Text;
                    profile["WorkPhone"].Value = tbPhone.Text;
                    profile["company"].Value = tbCompany.Text;
                    profile.Commit();
                }
                catch (Exception ex)
                {
                    lblMesssage.Text = ex.ToString() + "<br/>";
                    lblMesssage.Visible = true;
                }
        }
        catch (Exception ex)
        {
            lblMesssage.Text = ex.ToString();
            lblMesssage.Visible = true;
        }
  }
});
panComplete.Visible = true;
panForm.Visible = false;
waiting.Visible = false;
litSuccess.Visible = true;
}

couple of those things are in there as I've searched around and tried different things. Suggestions?


Solution

  • What we are doing at this point is writing to the hidden SP user profile list to spoof it until the AD sync takes place. This is not really the best answer but it's the only one I found so far. I'll happily accept a better answer if one comes.

    SPSecurity.RunWithElevatedPrivileges(() =>
                         {
                             using (var site = new SPSite(CurrentWeb.Site.Url))
                             {
                                 using (var web = site.OpenWeb(CurrentWeb.ID))
                                 {
                                     web.AllowUnsafeUpdates = true;
                                     SPList userInfo = web.Site.RootWeb.Lists["User Information List"];
                                     SPListItem userItem = userInfo.Items.GetItemById(_SelectedUser.ID);
                                     userItem["Work phone"] = tbPhone.Text;
                                     userItem["Work e-mail"] = tbEmail.Text;
                                     userItem["company"] = tbCompany.Text;
                                     userItem.Update();
                                 }
                             }
                         });