문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top