NieAR
NieAR

Reputation: 1445

Feature Event has 'null' objects

public class Feature1EventReceiver : SPFeatureReceiver
{

    public override void FeatureInstalled(SPFeatureReceiverProperties properties)
    {

        string sContextNull = (SPContext.Current == null) ? "Context is NULL" : "Context is OK";
        string sFeatureNull = (properties.Feature == null) ? "Feature is NULL" : "Feature is OK";

        // Some code here
        ...
        ...
    {
}

The feature has successfully installed (without error in logs). My problem is that sContextNull always returns "Context is NULL". And sFeatureNull always returns "Feature is NULL" too. Is there a way to get not null values of SPContext.Current and properties.Feature?

Another method FeatureActivated returns Context is NULL and Feature is OK. WTF?

Upvotes: 3

Views: 2189

Answers (3)

M.Ramadan
M.Ramadan

Reputation: 464

I think you need to get the web which contains this feature so try to use

(properties.Feature.Parent as SPWeb)

It works fine with me

Note:Try to cast it to SPSite if the feature scope is site

Upvotes: 1

NieAR
NieAR

Reputation: 1445

Probably properties.Feature in method FeatureInstalled is a bug. I have tried the next code and it works for me:

public class Feature1EventReceiver : SPFeatureReceiver
{    
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {    
        string sFeatureNull = (properties.Feature == null) ? "Feature is NULL" : "Feature is OK";

        // Some code here
        ...
        ...
    {
}

this method return Feature is OK.

Please avoid using properties.Feature in the method FeatureInstalled and FeatureUninstalling!!!

Upvotes: 1

djeeg
djeeg

Reputation: 6765

SPContext.Current

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontext.current.aspx

Gets the context of the current HTTP request in Microsoft SharePoint Foundation.

SPFeatureReceiver.FeatureInstalled is executing when a feature is installed into a farm, which is done with a deploy/install command from stsadm or powershell, which then usually triggers the timer job to do the work. At this point there is no HTTP request, so SPContext.Current returns null.

Upvotes: 2

Related Questions