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:
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:
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?