When ever you are trying to use user profiles in SharePoint , things can get tricky if you don’t get the settings right. Here i am discussing about an issue i recently faced when trying to update the custom user profile property in a Visual webpart in SharePoint 2010.Now you might face similar issue if you are trying to edit anything like user profile property, term store etc in GET requests.
Issue: Application throws an error “UPDATES ARE CURRENTLY DISALLOWED ON GET REQUESTS.TO ALLOW UPDATES ON A GET, SET THE ‘ALLOWUNSAFEUPDATES’ PROPERTY ON SPWEB ” when you are trying to update the User profile property on Page_Load.
Now i already set the allowunsafeupdates property to true, I checked whether the property is set to editable by user in User profile service and everything looked good but this problem doesn’t seem to resolve. After some more debugging i noticed that even though i have set allowunsafeupdates property to true , it automatically was reset to false by SharePoint , because allowunsafeupdates property is ignored in all GET requests.
Workaround: The workaround i got for this solution after trying of different methods is that temporarily set the current HttpContext to null. When the current HttpContext is null, SharePoint does not know that the request was a GET request and restore the HttpContext back to original after update.
HttpContext currentContext = HttpContext.Current;//store current context to a temp variable
HttpContext.Current = null;//set current context to null
currentProfile[“PropertyName”].Value = PropertyValue;//set property value
currentProfile.Commit();//update user profile
HttpContext.Current = currentContext;// reset the httpcontext
Hope this post saves your time..!!!
Leave a Reply