Cant use SPContext.Current.Site in Sandbox Feature Receiver

By , August 11, 2011

This may be common knowledge to you Sandbox Solution pros out there, but I banged my head against it for a while today and felt like sharing.

I was attempting to grab the current site using SPContext.Current.Site like I normally do, but I kept getting the generic Sandbox error tell me I was outside the confines of my sandbox.  This seems to only occur when dealing with feature receivers, but I am not positive about that.

As a work around, you can get the current site through the properties object as such.

SPSite siteCollection = properties.Feature.Parent as SPSite;

Happy coding!