Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer

"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: http://stackoverflow.com/questions/213882/best-pattern-for-allowunsafeupdates

share|improve this answer
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 '11 at 16:25
1  
More details - hristopavlov.wordpress.com/2008/05/16/… –  Ryan Dec 21 '11 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. –  CodingSheep Dec 21 '11 at 16:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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