Do I need to reset the SPWeb.AllowUnsafeUpdates property after use?

Seems not, but all over the web there is code like:

bool allowUnsafeOrig = web.AllowUnsafeUpdates;
web.AllowUnsafeUpdates = true;

// ....

web.AllowUnsafeUpdates = allowUnsafeOrig;

So, is it needed or not?

2 Answers 2


Ideally, you should neither set or reset the value of AllowUnsafeUpdates. Instead use the SPUtility.ValidateFormDigest and that's it. This sets AllowUnsafeUpdates = true for the current HTTP request.

The reason you should never set the AllowUnsafeUpdates = false is that other code might use SPUtility method an rely on that it was earlier asserted. Than your code runs in the meantime and sets the value to false which breaks the code.


"If you are going to use AllowUnsafeUpdates, there's no need to reset it to its previous value. It does not get persisted. It's just something you need to set on an SPWeb object before performing updates from a GET (or other cases)"

from this question: https://stackoverflow.com/questions/213882/best-pattern-for-allowunsafeupdates

  • 3
    Not sure about that - if you get SPWeb from Context.Current.Web then you're effectively sharing SPWeb with all other web parts on a page. If you set it, but don't reset it then what will the value be for other web parts, true/false? I don't know answer, just questioning.
    – Ryan
    Dec 21, 2011 at 16:25
  • 1
    More details - hristopavlov.wordpress.com/2008/05/16/…
    – Ryan
    Dec 21, 2011 at 16:27
  • @Ryan, that's a very good blog post you mentioned there, was very interesting to read it :) After doing so I still think that it's not necessary to reset it. But it also doesnt hurt to set it back to false after using it. I do that too, and will still do it, just because it feels saver.
    – LMW-HH
    Dec 21, 2011 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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