SPContext, anonymous users and SPRunWithElevatedPrivileges

An issue that we’ve come across recently while refactoring some code – rather than opening an SPSite and SPWeb we changed to just use SPContext.Site.Web as we couldn’t remember why we hadn’t used this in the first place. Initial tests were good – however when we tried to view the web part as an anonymous user we got prompted with a login box even though we were running the code in an SPRunWithElevatedPrivileges block. It seems that when you use the SPWeb directly from SPContext the code using the object will always run as the user viewing SharePoint – regardless of the elevated privileges.

This is only an issue for us when anonymous users are trying to update something – so general viewing is fine. This is also a good pointer of where a comment in the code would have been useful!

There is a good code sample here:

http://blogs.neudesic.com/blogs/jason_noble/archive/2007/04/01/5399.aspx

<nick/>