2

When I create a new site I have a Web Scoped FeatureActivated event that I would like to run. The feature does get Activated but the FeatureActivated event is not triggered.

The site is created with webtemplate that includes the features guid in the webfeatures node. From my webtemplate's onet.xml that specifies the feature

<WebFeatures>
   <Feature ID="e6e83174-0e6a-4fb6-a3e9-f16739a08a93" />
</WebFeatures>

Here is my featureactivated code:

private const string QuicklaunchHeader = "Saksmappe";

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb webTemp = properties.Feature.Parent as SPWeb;
    var web = webTemp.Site.RootWeb;
    var rootFolder = web.RootFolder;
    rootFolder.WelcomePage = "Pages/CaseFolder.aspx";
    rootFolder.Update();

    //Remove old custom quick launch entry
    RemoveQuickLaunchItems(QuicklaunchHeader, web);

    //Add custom quick launch entry
    var header = new SPNavigationNode(QuicklaunchHeader, 
                                       web.Navigation.Home.Url,   false);
    web.Navigation.QuickLaunch.AddAsFirst(header);

    //TODO Set correct urls
    header.Children.AddAsLast(new SPNavigationNode("Saksdetaljer", ""));
    header.Children.AddAsLast(new SPNavigationNode("Merknader", ""));
}

When I create the site I am not able to debug the code. I have tried Debugger.Break() and putting a break point in VS2010.

When I deploy the package from VS2010 the featureactivated code gets triggered. So the event does work, but not when I create the new site. Anyone knows how to solve this problem?

2
  • Is this your feature? Do you have the feature activated code? And can you debug? (Attach to OWSTIMER.EXE when you activate) Commented Jan 9, 2013 at 14:06
  • I have now edited my answer. It is my feature.
    – oftedal
    Commented Jan 9, 2013 at 14:41

1 Answer 1

2

You are using the rootweb of the site, not the current web, so any changing you are making is always to the rootweb. Instead of using webTemp to get rootweb, just use webTemp as the web.

private const string QuicklaunchHeader = "Saksmappe";

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb web = properties.Feature.Parent as SPWeb;
    var rootFolder = web.RootFolder;
    rootFolder.WelcomePage = "Pages/CaseFolder.aspx";
    rootFolder.Update();

    //Remove old custom quick launch entry
    RemoveQuickLaunchItems(QuicklaunchHeader, web);

    //Add custom quick launch entry
    var header = new SPNavigationNode(QuicklaunchHeader, 
                                       web.Navigation.Home.Url,   false);
    web.Navigation.QuickLaunch.AddAsFirst(header);

    //TODO Set correct urls
    header.Children.AddAsLast(new SPNavigationNode("Saksdetaljer", ""));
    header.Children.AddAsLast(new SPNavigationNode("Merknader", ""));
}
4
  • 1
    I have now changed my code to your suggestion and it now throws the following error. 'FeatureActivated' for feature 'e6e83174-0e6a-4fb6-a3e9-f16739a08a93' threw an exception: System.NullReferenceException: Object reference not set to an instance of an object.
    – oftedal
    Commented Jan 9, 2013 at 15:08
  • Can you debug it and see what is null? Commented Jan 9, 2013 at 15:12
  • 1
    web.Navigation.Home is null.
    – oftedal
    Commented Jan 9, 2013 at 15:22
  • you can use web.Site.RootWeb.Navigation.Home to get that value Commented Jan 9, 2013 at 15:36

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.