4

I have a feature for deploying masterpages that I am trying to activate upon install(deploy). The following snippets of code are in the EventReceiver.cs. I am getting a null when attempting to reference properties.Feature (the error actually occurs when calling .Parent since properties.Feature is null. How do I reference the current site in the FeatureInstalled to Activate it? SPContext.Current also returns null in Feature installed.

public static class Extensions
    {
  public static SPWeb GetWeb(this SPFeatureReceiverProperties properties)
    {
            SPWeb site;


            if (properties.Feature.Parent is SPWeb)
            {
                site = (SPWeb)properties.Feature.Parent;
            }
            else if (properties.Feature.Parent is SPSite)
            {
                site = ((SPSite)properties.Feature.Parent).RootWeb;
            }
            else
            {
                throw new Exception("SPWeb not available - Feature is not Site or Web Scoped.");
            }
            return site;
       }
       }

public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {

            SPWeb curweb = properties.GetWeb();
            curweb.Features.Add(new Guid("{GUID_HERE}"));
     }

1 Answer 1

4

The FeatureInstalled method is called when the WSP is loaded into the Farm and the Feature files are copied into the 12 hive.

This is done in Central Administration or via the command line (Powershell or STSADM). Because of this, there is no website Context to speak of, no site collection or no SPWeb.

5
  • If context is not known in FeatureInstalled, how do I activate the feature during deployment? My goal is to Add-SPSolution and Install-SPSolution and have the feature activated in every site collection in the Web app. Aug 2, 2011 at 15:33
  • Then your solution is to have the Feature scoped to Web-Application, your Feature.Parent will be an SPWebApplication object, and you will be able to access the Sites property of that SPWebApplication, which is a collection of SPSite objects. You'll then be able to activate your Site Collection Scoped feature at that level.
    – James Love
    Aug 2, 2011 at 15:43
  • Additionally, I should clarify that originally I intended to say "No web site context" rather than blanket "no context".
    – James Love
    Aug 2, 2011 at 15:44
  • That makes sense. This particular feature is for deploying custom masterpages and when I change the scope to WebApplication it get 'The Project Item "_catalogs" cannot be deployed through a Feature with WebApplication scope'. Aug 2, 2011 at 16:21
  • Yeah, you need two separate Features. One is WebApplication scoped, and has a Feature Receiver which will get a list of all the Site Collections in that Web Application, and activate a separate, Site Collection scoped feature, which will deploy the masterpage.
    – James Love
    Aug 2, 2011 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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