none
How to: Update a User Profile

    Question

  • Hi

    I am trying below code in Visual Web Part.

    using (SPSite site = new SPSite("http://servername"))
    {
    SPServiceContext context = SPServiceContext.GetContext(site);
    UserProfileManager profileManager = new UserProfileManager(context);
    string sAccount = "domainname\\username";
    UserProfile u = profileManager.GetUserProfile(sAccount);
    //Updates values
    u[PropertyConstants.HomePhone].Value = "nnnnnnnnnn";
    u[PropertyConstants.CellPhone].Value = "nnnnnnnnnn";
    //commits changes
    u.Commit();
    }
    But got this error

    "Updates are currently disallowed on GET requests.  To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb."

    Please help me to resolve this issue.

     

    Thanks

    Joy Jose

    Monday, November 29, 2010 10:04 AM

Answers

  • Hi, Jose

     

        In my mind, you should firstly check the permission your code running for retrieve User Profile information.

        If you use SPSecurity.RunWithElevatedPrivileges Method for running the code block under the Application Pool identity, which has site collection administrator privileges on all site collections hosted by that application pool.

        In Ganesh’s code snippet, UserProfileManager object is used, so please authorize permission(in ServiceApplications-->User Profile Service Application--> Manage User Permission) to this site collection administrator.

    Hope this can help.

     

    Best Regards,

    Aaron

    • Marked as answer by Wayne Fan Friday, December 10, 2010 1:39 AM
    Friday, December 03, 2010 9:24 AM

All replies

  • Plz try this

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        SPSite site = new SPSite("http://servername");
                  ServerContext context = ServerContext.GetContext(site);
                  UserProfileManager profileManager = new UserProfileManager(context);
        string sAccount = "domainname\\username";
                  UserProfile userProfile;
                  if (profileManager.UserExists(sAccount ))
                  {
                      userProfile = profileManager.GetUserProfile(sAccount);
                      userProfile[PropertyConstants.HomePhone].Value = "nnnnnnnnnn";
                      userProfile.Commit();
                  }
    });



    Thanks
    Ganesh Jat [My Blog | LinkedIn | Twitter ]
    Monday, November 29, 2010 11:33 AM
  • Hi Joy Jose

    I got the same error when i tried to update my profile via client - side (ajax calling) using jquery ,So if that's your case ( just assuming ) you can set AllowUnsafeUpdates= true as following

    using (SPSite site = new SPSite("http://servername"))
    
     {
     site.AllowUnsafeUpdates=true;
      SPServiceContext context = 
     SPServiceContext.GetContext(site);
    
     UserProfileManager profileManager = new UserProfileManager(context);
    
     string sAccount = "domainname\\username";
    
      UserProfile u = profileManager.GetUserProfile(sAccount);
    
      //Updates values
      u[PropertyConstants.HomePhone].Value = "nnnnnnnnnn";
      u[PropertyConstants.CellPhone].Value = "nnnnnnnnnn";
      //commits changes
      u.Commit();
     //after changes completed set AllowUnsafeUpdate to false
    site.AllowUnsafeUpdates=false;
    
     }

    For more information check the below posts

    http://social.msdn.microsoft.com/Forums/en/sharepointinfopath/thread/9f040077-87f9-40d1-a80d-ec7677bf2093

    http://www.sharepointoverflow.com/questions/1594/updates-are-currently-disallowed-on-get-requests-error-creating-a-site-collecti

    Note: You cannot update other user profiles unless you have administration rights on user profile service

    Regards,


    Ahmed Naji SharePoint Geek
    MCP|MCTS
    My Blog | DotnetFinder
    Monday, November 29, 2010 11:39 AM
  • Hi Ganesh jat, Alnaji

    I already tried these two methods. Still the code is throwing the same exception.

    Thanks

    Joy Jose

    Monday, November 29, 2010 12:25 PM
  • Hi, Jose

     

        In my mind, you should firstly check the permission your code running for retrieve User Profile information.

        If you use SPSecurity.RunWithElevatedPrivileges Method for running the code block under the Application Pool identity, which has site collection administrator privileges on all site collections hosted by that application pool.

        In Ganesh’s code snippet, UserProfileManager object is used, so please authorize permission(in ServiceApplications-->User Profile Service Application--> Manage User Permission) to this site collection administrator.

    Hope this can help.

     

    Best Regards,

    Aaron

    • Marked as answer by Wayne Fan Friday, December 10, 2010 1:39 AM
    Friday, December 03, 2010 9:24 AM