Question

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}"));
     }
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top