Вопрос

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();
//}
  }
}
Это было полезно?

Решение 2

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;

}

Другие советы

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

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.

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;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top