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

sharepoint

I have a hidden webpart that read query string value "optout=Yes" . This optout = Yes, then I need to update profile property. If you see in my code. It is failing at "userprof.Commit()" and throwing "Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb" . Any solution for this?

private void OptOutMemberInvitation()
{

  SPSecurity.RunWithElevatedPrivileges(delegate()
  {

    //update the invitee's Profile property
    UpdateInviteeOptOutProfile(InviteeConstitID);

  });
}
private void UpdateInviteeOptOutProfile(string inviteeSiteColUrl)
{
  ServerContext sc = ServerContext.Current;
  UserProfileManager upm = new UserProfileManager(sc);
  //Get the user profile
  Microsoft.Office.Server.UserProfiles.UserProfile userprof = upm.GetUserProfile(MemberConstitID);
  SPWeb web = userprof.PersonalSite.RootWeb;

  //make sure we can update our list
  web.AllowUnsafeUpdates = true;
  web.Update();
  //Update the OptOut Property on the user's profile.
  userprof["OptOut"].Value = "Yes";
  userprof.Commit(); //Fails here
  //update the list item to persist it to list

  web.AllowUnsafeUpdates = false;
  //siteCol.Close();
  //siteCol.Dispose();
}

Best Answer

We have used "SPSecurity.RunWithElevatedPrivileges" that means we would like to use app pool account context for this update process. But inside the function "UpdateInviteeOptOutProfile" we have used current context instead of creating a new site>web object.

Please create a new site and then web object using the URL Or ID.

Related Topic