Question

This is my sample code:

SPWeb web = SPContext.Current.Web
SPList list = web.Lists["TestList"];

try {
    web.AllowUnsafeUpdates = true;
    list.Title = "Test";
    list.Update();
}

finally {
    web.AllowUnsafeUpdates = false;
}

I removed finally code block, then tried it again. I found "web.AllowUnsafeUpdates" is still false. Does it mean setting web.AllowUnsafeUpdates = false is not necessary?

Was it helpful?

Solution

web.AllowUnsafeUpdates is set for duration of request (for SPContext.Current.Web) or for lifetime of corresponding SPWeb object if one is obtained from SPSite via GetWeb(...) or Webs[].

Depending on what "tried again" means you could be getting fresh copy of SPWeb for new request.

It is good practice to keep AllowUnsafeUpdates to be tru for very scoped pieces of code as you do with try/finally. There could be other code that would run during the same request and by mistake be able to update objects when it should not be allowed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top