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

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 web part 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 web part to the page and modifying the navigation (or trying to). The web part 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);

        webParts.SaveChanges(wp);

        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.

share|improve this question
add comment

1 Answer

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.

share|improve this answer
    
I've written a blog to explain clearer here cann0nf0dder.wordpress.com/2013/10/08/… –  Cann0nF0dder Oct 8 '13 at 14:59
add comment

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.