1

I am having problems with adding navigation links via a Feature Receiver. Just as a background, I have a feature (FeatConfigureNav) which is activated automatically via a web template. This sis a sample code I have:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPWeb web = properties.Feature.Parent as SPWeb;

        NavHelper.DeleteTopNavAllNodes(web);

        web.Update();
    }
...
...
public static void DeleteTopNavAllNodes(SPWeb web)
    {
        //get items in quick launch navigation
        SPNavigationNodeCollection topNav = web.Navigation.TopNavigationBar;

        int ctr = 0;

        //delete all items
        while (topNav.Count != ctr && topNav.Count > 0)
        {
            ctr++;
            topNav[0].Delete(); //delete items one by one
        }
    }

The problem is that when deploying the solution through Visual Studio and creating the site via the web template, I am getting an error that the TopNavigationBar is NULL as seen in image below:

enter image description here

However, I have also tried removing the feature from the web template and just manually activating the feature through the browser instead after the site has been created. I deployed my solution, ensured the site was created, then attached to a process to debug my code and activated the FeatConfigureNav. Now I find that the TopNavigationBar has the proper object:

enter image description here

Why does this happen? I need to have the Feature automatically activate and work via the web template and Visual Studio deployment since that is going to be a hidden feature. Is there a reason that the TopNavigationBar has NULL value and how can I fix it?

  • You have to execute TopNavigationBar code when site created successfully. You can active "FeatConfigureNav" after site created. – Bharat Khunti Jun 29 '17 at 11:43
  • Hello! Is there a way to handle an event like that like "SiteCreated"? Where can I place code that will execute after a site has been created? – bloodfire1004 Jun 29 '17 at 11:47
  • Refer this link vrdmn.com/2014/03/create-site-collections-with-csom-in.html May be this will help you – Bharat Khunti Jun 29 '17 at 14:19
  • The issue here is that web.Navigation.TopNavigationBar is null when you are not in the "browser" context. I have the same issue when using C# console or PowerShell web.Navigation.TopNavigationBar is null. At least after the site was just created and the navigation was never updated via the browser. – Denis Molodtsov May 30 '18 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.