I am trying to develop a Sharepoint 2013 feature to add several navigation nodes in the top navigation bar of the web site on which the feature is activated. To achieve this, I have overriden only the FeatureActivated method of the event receiver. In case the feature is activated in a top level site, only two navigation nodes would be added to the bar (the main page node and the current site node). But if the feature is activated within a subsite, three navigation nodes should be added instead (main page, current site and subsite). My source code is as follows:
public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPWeb web = (SPWeb)properties.Feature.Parent; SPNavigationNode node = new SPNavigationNode("Main", "http://homepage", true); web.Navigation.TopNavigationBar.AddAsLast(node); if (!web.IsRootWeb) { SPNavigationNode node1 = new SPNavigationNode("Site 1", web.ParentWeb.Url, true); web.Navigation.TopNavigationBar.AddAsLast(node1); } SPNavigationNode node2 = new SPNavigationNode("Site 2", web.Url, true); web.Navigation.TopNavigationBar.AddAsLast(node2); }
The problem I'm facing is that, when activated, the feature only adds the first ("Main") navigation node. I decided to remove the if statement and deploy the following code for testing purposes:
public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPWeb web = (SPWeb)properties.Feature.Parent; SPNavigationNode node = new SPNavigationNode("Main", "http://homepage", true); web.Navigation.TopNavigationBar.AddAsLast(node); SPNavigationNode node2 = new SPNavigationNode("Site 2", web.Url, true); web.Navigation.TopNavigationBar.AddAsLast(node2); }
But again, only the first navigation node ("Main") was added. That made me think that the code was probably throwing an error when retrieving the url. Because of that, I decided to deploy the following code that basically puts null instead of an
actual url and uses the web site url as the display name for both site and subsite nodes, just for the sake of testing:
public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPWeb web = (SPWeb)properties.Feature.Parent; SPNavigationNode node = new SPNavigationNode("Main", "http://homepage", true); web.Navigation.TopNavigationBar.AddAsLast(node); if (!web.IsRootWeb) { SPNavigationNode node1 = new SPNavigationNode(web.ParentWeb.Url, null, true); web.Navigation.TopNavigationBar.AddAsLast(node1); } SPNavigationNode node2 = new SPNavigationNode(web.Url, null, true); web.Navigation.TopNavigationBar.AddAsLast(node2); }
This time, only the site and subsite nodes where added (and not the "Main" node). By the way, the urls were correctly printed. Can anyone help me understand what is causing this behavior?