Writing a web part recently, I was attempting to set some default values for locale sensitive properties. I wanted them set early in the webpart lifecycle so I figured I’d do it in the web part constructor.
Since they are locale sensitive I was using the SPUtility.GeLocalizedString method to fetch the value from resources.
When I tried adding the web part to a page, I’d get a security exception which indicated that “access to the file is denied”. Among other things the exception also contained various security link demand violations.
Thanks to an obscure post I found on a forum somewhere, it turns out that you can’t call the SharePoint object model from the constructor, and moving the offending calls to another location (say OnInit) fixed the problem.
Published by