I am using feature stapling to add a feature to all sites in a web application. This feature creates a list, creates a site page, creates a WebPart, adds the WebPart to the site page, and then it is suppose to add the link to the page to the nav bar.

Everything is working except for the changes to the nav bar. When I manually activate the feature it works but when activated through feature stapling it does not.

Here is the code I am using for adding the WebPart to the page and modifying the navigation (or trying to). The WebPart is added to the page so I know that it is being fired.

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
        SPSite site = properties.Feature.Parent as SPSite;
        SPWeb web = site.RootWeb;
        web.AllowUnsafeUpdates = true;

        SPLimitedWebPartManager webParts = web.GetLimitedWebPartManager("AboutPages/AboutPage.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

        AboutInformation.AboutInformation wp = new AboutInformation.AboutInformation();
        wp.Title = "About";
        wp.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;
        webParts.AddWebPart(wp, "Left", 0);


        web.Navigation.TopNavigationBar.AddAsLast(new Microsoft.SharePoint.Navigation.SPNavigationNode("About", @"AboutPages/AboutPage.aspx"));
        web.Navigation.TopNavigationBar.AddAsLast(new Microsoft.SharePoint.Navigation.SPNavigationNode("Google", @"http://www.google.com", true));

        web.Navigation.QuickLaunch.AddAsLast(new Microsoft.SharePoint.Navigation.SPNavigationNode("About", @"AboutPages/AboutPage.aspx"));
        web.Navigation.QuickLaunch.AddAsLast(new Microsoft.SharePoint.Navigation.SPNavigationNode("Google", @"http://www.google.com", true));


EDIT: I am adding the AboutPage.aspx in the feature that the above is called so if it is possible to add to navigation declaratively instead of programmatically that would work also. The google nav is in there just for testing purposes to see if it was an issue with AboutPage not being fully provisioned.

1 Answer 1


I know this is an old question but I hope this might offer an alternative.

The alternative is to get the first user who hits the page fire the event to add your navigation.

  1. Create a Control Template called Initialiser.ascx. It has a public property galled FeatureGuid. Inside the Page Load event you perform the work of adding the navigation. Elevate permission if you have too to get the job done. If the job completes successfully, then remove the feature based on the FeatureGuid.

    web.features.Remove(new Guid(FeatureGuid)

  2. Create a delegate control that points to the control template. The ID is the AdditionalPageHead. Include

    {guid of feature}

  3. Create a feature that holds the delegate control. This features ID is the ID you put in the property FeatureGuid in the delegate control.

  4. Add this feature to your stapler.

So when the site is created the control template sits there waiting. The first user who hits it runs the code, adds the bits you can't seem to add via the feature stapler when the site is being created, then it deactivates itself so that the Control Template gets removed from the site and no chance of running the code again.


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.