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!

2 Responses to “Cant use SPContext.Current.Site in Sandbox Feature Receiver”

  1. marut says:

    Hi Steve
    I am developing a sandbox solution for custom webparts and will be using it on office365. As ‘Run with Elevated Priveliges’ do not works in sandboxing, please help me to know its alternative.
    And even i doesn’t get any solution for usertoken insandbox solution that is how to use it in sandbox solution
    Pllz help

  2. Bart says:

    Thank you very much for sharing this information :)

Leave a Reply