Scenario:
One of my friend asked me how to add a new item to Top Navigation Bar.
Solution:
You can write a feature receiver and using Object model can update the Top Navigation Bar.
Code:
SPWeb web = SPContext.Current.Web;Errors:
web.AllowUnsafeUpdates = true;
if (!web.Navigation.UseShared)
{
SPNavigationNodeCollection topNavigationNodes = web.Navigation.TopNavigationBar;
// You can also edit the Quick Launch the same way
//SPNavigationNodeCollection topNavigationNodes = web.Navigation.QuickLaunch;
SPNavigationNode objItem = new SPNavigationNode("MyWorkspace", "default.aspx", false);
topNavigationNodes.AddAsFirst(objItem);
}
web.Update();
web.AllowUnsafeUpdates = false;
If you try to put have a absolute url , you might get an error like this
Cannot open "/http://localhost/default.aspx": no such file or folder.
Also if you miss the check for the Shared Navigation , then you will get the following error
The top link bar can only be edited for a Web site with unique navigation.
5 comments:
Hi,
I have problem when I update the Name and Title field of SPWeb object. TopNavigation bar doesn't get updated.
Can you tell me how to achieve this?
Site Action > Site Settings > Top Navigation or Navigation and edit the Menu the way u want
I have try using this code on several VM with no success.
I can see the topNavigationBar collection count incrementing by 1 each I run it. But the item(s) will not appear when view it either normally UI or in the navigation settings page.
Please assign.
I have try using this code on several VM with no success.
I can see the topNavigationBar collection count incrementing by 1 each I run it. But the item(s) will not appear when view it either normally UI or in the navigation settings page.
Please assign.
Hi,
I have a same requirment but i want to do it from diffrent site collection with in same webapplication.
Please help
Post a Comment