Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to update the user profile details through the code however it is throwing the below exception "Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb".

what am I missing? and how to fix?

Code

private void updateUserProfileChanges(string _mySiteLocal, string _mySiteCentral)
{  
  string currentUser ="";
  string localLastUpdateDate ="";

  currentUser = SPContext.Current.Web.CurrentUser.LoginName;

  using (SPSite localMySite = new SPSite(_mySiteLocal))
  {
    //using(SPWeb web = localMySite.OpenWeb())
    //{
    //web.AllowUnsafeUpdates = true; 
    //web.Update();
    SPSecurity.RunWithElevatedPrivileges(delegate
    {
    SPServiceContext localContext = SPServiceContext.GetContext(localMySite);
        UserProfileManager localProfileManager = new UserProfileManager(localContext); 
        UserProfile localUserProfile = localProfileManager.GetUserProfile(currentUser); 

    try
    {
        if (localUserProfile.GetProfileValueCollection("LocalLastUpdateDate").Count >= 1)
        {
           localLastUpdateDate = localUserProfile["LocalLastUpdateDate"].ToString();
        }
        else
        {
        localLastUpdateDate = DateTime.Now.ToString();
        localUserProfile["CellPhone"].Value = "nnnnnnnnnnn";
        localUserProfile.Commit(); //throws an exception
        }
    }   
    catch(Exception ex)
    {

    }        
});
//web.AllowUnsafeUpdates = false;
//web.Update();
//}
  }
}
share|improve this question
    
same kind of question is asked here without an answer - stackoverflow.com/questions/2694995/…. –  Karthikeyan Sep 27 '11 at 16:19
    
Where do you call this code from? A little note: The localMySite in your code is not elevated, because you do not new the SPSite within the delegate.. –  Bernd Rickenberg Sep 27 '11 at 16:29
    
do you mean the localmysite instance should be created inside the delegation function? simply calling this code from the main function. –  Karthikeyan Sep 27 '11 at 16:57
    
solved the issue...wss.boman.biz/Lists/Posts/Post.aspx?ID=23 –  Karthikeyan Sep 28 '11 at 7:27
    
however do not know why do we need to do it? any help. –  Karthikeyan Sep 28 '11 at 7:28
add comment

3 Answers 3

Try this code:

private void updateUserProfileChanges(string accountName)
{  
    string localLastUpdateDate ="";

    SPSecurity.RunWithElevatedPrivileges(delegate
    {
        string centralAdminUrl = SPAdministrationWebApplication.Local.AlternateUrls[0].IncomingUrl;
        using (SPSite ca = new SPSite(centralAdminUrl))
        {
            SPServiceContext ctx = SPServiceContext.GetContext(ca);
            UserProfileManager mng = new UserProfileManager(ctx);
            UserProfile profile = mng.GetUserProfile(accountName);

            // I just copied this part
            if (profile.GetProfileValueCollection("LocalLastUpdateDate").Count >= 1)
            {
                 localLastUpdateDate = profile["LocalLastUpdateDate"].ToString();
            }
            else
            {
                 localLastUpdateDate = DateTime.Now.ToString();
                 profile["CellPhone"].Value = "nnnnnnnnnnn";
                 profile.Commit();
            }
        }
    });
}

I have removed try catch block only to provide more readable code!

Also you need to make sure that your Farm Account has permissions to update user profiles!

Most likely you can drop RunWithElevatedPrivileges if you are updating current user settings because in most scenarios it is allowed - or better, it should be allowed. However it depends on your requirements and settings.

share|improve this answer
add comment

I am getting this Error when I am updating the SPItem as below: NOTE: the Web object is initialized through SPControl.getContextWeb(context);

this.Web.AllowUnsafeUpdates = true;
this.spItemObeject.Update();
this.Web.AllowUnsafeUpdates = false;

so i finally changed the code as below its worked fine:

spItemObeject.Web.AllowUnsafeUpdates = true;
this.spItemObeject.Update();
spItemObeject.Web.AllowUnsafeUpdates = false;

Thanks

Suresh T G

share|improve this answer
add comment
up vote 0 down vote accepted
using (SPSite localMySite = new SPSite(_mySiteLocal,userToken ))
{
    SPServiceContext localContext = SPServiceContext.GetContext(localMySite);
    UserProfileManager localProfileManager = new UserProfileManager(localContext); 
    UserProfile localUserProfile = localProfileManager.GetUserProfile(currentUser); 

    HttpContext currentContext = HttpContext.Current;
    HttpContext.Current = null;

    localUserProfile["LocalLastUpdateDate"].Value = centalLastUpdateDate ;
    localLastUpdateDate = centalLastUpdateDate ;
    localUserProfile.Commit();

    HttpContext.Current = currentContext;

}

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.