While working on some enhancements for the Site Configurator Feature we just released to CodePlex, I was struggling with an unexpected behavior when trying to add create SPNavigationNodes programmatically for quick launch and top navigation links. There are two constructors to select from:
public SPNavigationNode(string title, string url)
public SPNavigationNode(string title, string url, bool isExternal)
Since I needed to support both internal and external links, I chose the second one. Adding external links and links to subsites works fine, but adding links that point to the same site seems to fail. In the following example I am trying to create a duplicate of the out-of-the-box "Lists" quick launch heading.
SPNavigationNode node = new SPNavigationNode("MyInternalLink", "/_layouts/viewlsts.aspx?BaseType=0", false);
The link will be added like this: http://_layouts/viewlsts.aspx?BaseType=0. Which obviously is invalid. So how did I come around this? Using .NET Reflector I checked the OnSubmit method of the NavNodeCreatePage class (NavNodeCreatePage is the codebehind for newnav.aspx). This is the page used when adding navigation nodes in the user interface. Interestingly I found that the "isExternal" parameter was "hardcoded" to "true". So I changed my code to:
SPNavigationNode node = new SPNavigationNode("MyInternalLink", "/_layouts/viewlsts.aspx?BaseType=0", true); web.Navigation.QuickLaunch.AddAsLast(node);
It does not make sense to me, but at least it solved the problem. If anyone can explain it, please leave a comment.