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?

4
  • You have to execute TopNavigationBar code when site created successfully. You can active "FeatConfigureNav" after site created. Commented Jun 29, 2017 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? Commented Jun 29, 2017 at 11:47
  • Refer this link vrdmn.com/2014/03/create-site-collections-with-csom-in.html May be this will help you Commented Jun 29, 2017 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. Commented May 30, 2018 at 17:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.