Tell me more ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

While testing my solution I receive an error on Feature activation:

Detected use of SPRequest for previously closed SPWeb object. Please close SPWeb objects when you are done with all objects obtained from them, but not before.

This is the way I'm getting SPWeb:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    using (SPWeb web = properties.Feature.Parent as SPWeb)
    {
        ClassOfMine.doYourStuff(web);
    }
}

What am I doing wrong?

share|improve this question

1 Answer

up vote 2 down vote accepted

how about;

warning: your feature needs to be scoped as web for it to work obviously ;)

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
  // No need to dispose the web istance, as indicated in the "Do not dispose" guidance
  SPWeb web = (SPWeb) properties.Feature.Parent      

  ClassOfMine.doYourStuff(web);

}

if not then use the spcontext to get the root web

SPContext.Current.Site.RootWeb

or - for the current web

SPContext.Current.Web

or - for a specific web url

SPContext.Current.Site.OpenWeb("Website_URL"))
share|improve this answer
SPContext.Current.Site.RootWeb worked for me in a way. Thanks :-) – Marius Kay Jan 18 at 15:37
+1 for the answer, I only edited a little imprecision. Hope you don't mind (as told you on meta, I prefer to improve a valid answer ^_^). See this link for other info blogs.msdn.com/b/rogerla/archive/2009/11/30/… – SPArchaeologist Jan 18 at 16:06
lol no probs ;) – ali Sharepoint Jan 18 at 16:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.