Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting SPContext inside event receiver

I created an Event Receiver, but the problem is that I can't get a reference to the SPContext: SPContext.Current returns null. I need it to add some lists to the site. Does anyone have an idea of how I could get it?

Also I tried putting break points inside the event receiver, but FeatureActivates never triggers for some reason. What is the correct event to use when a list is activated right after de deployment?

like image 319
Dr.Denis McCracleJizz Avatar asked Feb 27 '12 16:02

Dr.Denis McCracleJizz


1 Answers

You can't get SPContext inside handlers - this is by design. You should use event properties passed as argument to the handler to get reference to current web, list item etc. For example in feature activated handler you can do it like this:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb web = properties.Feature.Parent as SPWeb;  
    //Some code with web
}

If Feature Scope is Site then

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
        SPSite site = properties.Feature.Parent as SPSite;  
        //Some code with web
}
like image 128
Ivan Vagunin Avatar answered Sep 17 '22 17:09

Ivan Vagunin