In a solution I'm building I was met with the task of creating a web site programmaticly and no sweat I thought. The site creation itself was a breeze, but I wsa met a bit of a challange when trying to create navigation tabs.
The parent web site would have a tab on the top navigation bar pointing to the new web site, and the newly created web site would have a tab as well that was highlighted in the top navigation when the user was visiting the site.
No problem I thought again. So I happily went and created the tab to the new site on the parent web site and told the newly created website to just inherit navigation. But to my surprise the tab on the newly created site was not highlighted when I was visiting it. This kinda puzzled me.
I had told the top navigation bar of the parent web site to use this node:
SPNavigationNode navigationNode = new SPNavigationNode("My Web Site", "/MyWebSite");
But apparently that wasn't enough to highlight the tab.
After some more efford I finally figured out that it had to be:
SPNavigationNode navigationNode = new SPNavigationNode("My Web Site", "/MyWebSite/default.aspx");
Yeah .. you have to remember default.aspx. Anyway the code to create a child web site with parent top navigation and tab highlighting would be:
using (SPSite site = new SPSite(siteUrl)) {
using (SPWeb web = site.AllWebs.Add("/MyWebSite", "My Web Site", "", 1033, "STS#1", false, false)) {
SPNavigationNode navigationNode = new SPNavigationNode("My Web Site", "/MyWebSite/default.aspx"); SPNavigation navigation = web.ParentWeb.Navigation; SPNavigationNodeCollection topNavigationNodeCollection = navigation.TopNavigationBar; topNavigationNodeCollection.AddAsLast(navigationNode);
web.Navigation.UseShared =
true; web.Update();
}
}
Happy SharePoint hacking.