3

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

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

1

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.

1
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;

}

0

Neither only context nor only allowunsafe property worked for me

Sort Answer: Use my made method doUnSafeOperations (copy paste it) pass your method (having update instructions to it as

    doUnSafeOperations(() =>
    {
        SPListItem spListItem = oList.Items.Add();
        spListItem["Title"] = "Hello SharePoint";
        spListItem.Update();
    });
    //() => is way to make a method/action without name. But both did

Complete example

    SPWeb mySite = SPContext.Current.Web;
    SPListCollection lists = mySite.Lists;
    SPList oList = lists["SomeList"];


    doUnSafeOperations(() =>
    {
        SPListItem spListItem = oList.Items.Add();
        spListItem["Title"] = "Hello SharePoint";
        spListItem.Update();
    });


public void doUnSafeOperations(Action updateOperation)
{
    HttpContext currentContext = HttpContext.Current;
    HttpContext.Current = null;
    //Using try catch is compulsory here. To be sure that
    //you set your changed properties back, even if your operation fails
    try{
    updateOperation();
    }catch{}
    HttpContext.Current = currentContext;
    mySite.AllowUnsafeUpdates = false;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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