SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;
                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}"));
share|improve this question
up vote 4 down vote accepted

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.

share|improve this answer
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. – Jason Jones Aug 2 '11 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 '11 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 '11 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'. – Jason Jones Aug 2 '11 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 '11 at 16:35

Your Answer


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.