Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
    
Is this your feature? Do you have the feature activated code? And can you debug? (Attach to OWSTIMER.EXE when you activate) –  thantos Jan 9 '13 at 14:06
    
I have now edited my answer. It is my feature. –  Kristian Jan 9 '13 at 14:41

1 Answer 1

up vote 1 down vote accepted

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", ""));
}
share|improve this answer
    
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. –  Kristian Jan 9 '13 at 15:08
    
Can you debug it and see what is null? –  thantos Jan 9 '13 at 15:12
    
web.Navigation.Home is null. –  Kristian Jan 9 '13 at 15:22
    
you can use web.Site.RootWeb.Navigation.Home to get that value –  thantos Jan 9 '13 at 15:36
    
that works, thanks! –  Kristian Jan 9 '13 at 16:27

Your Answer

 
discard

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.