6

When & why I need to use web.AllowUnsafeUpdates = true, and why it is false by default:

 web.AllowUnsafeUpdates = true;
 //Update Item
 web.AllowUnsafeUpdates = false;
  • 1
    In a few rare scenarios, blindly setting it to false after doing the updates may caues issues, with other out-of-the-box code or with other custom code. It is a safer habit to save the original value Before changing it, then changing it back to the original value when done. – JanErikGunnar Apr 2 '17 at 21:50
6

For reasons of security, by default, Microsoft SharePoint Foundation does not allow you to make posts from a Web application to modify the contents of the database unless you include security validation on the page making the request. You can update data for a single site or for a site collection by adding a page directive and a FormDigest control to the page that makes the request.

To GET the content from the content DB, we need to set the AllowUnsafeUpdates = true. To POST the content to the content DB, not require to set the AllowUnsafeUpdates = true. why means because of "FormDigest" control placed in every master page.

When/where/why use "AllowUnsafeUpdates" in SharePoint?

Why do I need to use SPWeb.AllowUnsafeUpdates?

What You Need To Know About AllowUnsafeUpdates (Part 1)

  • 5
    This answer is correct from a technical perspective. However, there are no valid scenarios in which custom code should use AllowUnsafe. If you are writing custom code, you should use the FormDigest or first make a call to _api/contextinfo. – Paul Schaeflein Apr 7 '15 at 1:39
  • 2
    I think there's still an error. You need unsafe updates allowed to make updates during the processing of a GET request, not to GET data from database. – Alexey Krasheninnikov Apr 8 '15 at 7:46
  • unfortunately i can't understand the answer, it seem correct, but is there an simple way to describe it. why we don't need something like this with ASP.net Applications? – jobin Apr 9 '15 at 7:24
  • @jobin because SharePoint. – Ashley Steel Apr 9 '15 at 20:32
  • @jobin Say you have a list and you want to update something to the SharePoint data [content DB], then you need to set AllowUnsafeUpdates = true for the web and after you have done you need to set it back to false. – Waqas Sarwar MVP Apr 9 '15 at 20:39
4

WHY --> to prevent your code from cross site scripting.

If you are updating content DB using a POST request then you should use SPUtility.ValidateFormDigest() to validate the current request and after that you will not have to worry about AllowUnsafeUpdates, because its default value will be “true” after the form digest is validated. The Microsoft idea behind introducing the AllowUnsafeUpdates property is to protect you from cross-site scripting attacks.

If you are using GET request to update the sharepoint objects then it will throw an exception System.Exception: Microsoft.SharePoint.SPException bcoz your code will be exploitable via a cross-site scripting. To prevent this CSS you have to set AllowUnsafeUpdates to true and after changes make it to false or whatever the previous value.

Also If the HTTPContext.Current is null then AllowSafeUpdates will be always true. This is the case in rich clients where no cross-scripting is possible as there are simply no web requests.

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.