In SharePoint 2007 users have several options to include or hide pages in the navigation of a publishing site. In one of my projects I had to set these options from code. It took me some time to find out how to do it in a way that worked in all their sites, so I decided to write it down in this blog post. All code samples below use the CurrentNavigation. Setting navigation options for pages is done in a PublishingWeb object. Below you will find the code to find the navigation options for your site:
using (SPSite site = new SPSite("http://moss/navigation"))
{
using (SPWeb web = site.OpenWeb())
{
string pageUrl = "Page2.aspx";
bool show = false;
PublishingWeb pw = Microsoft.SharePoint.Publishing.PublishingWeb.GetPublishingWeb(web);
PublishingPageCollection webpages = pw.GetPublishingPages();
PublishingPage page = webpages[string.Format("Pages/{0}", pageUrl)];
}
}
Hide a page from the navigation
Suppose we have created a new Publishing site that has 3 pages, and the Navigation option "Show Pages" is switched on:
If we now want to hide Page 2 from the navigation, the easiest thing to do is call "ExcludeFromNavigation" on the publishingweb:
pw.ExcludeFromNavigation(false, page.ListItem.UniqueId);
pw.Update();
This has the same effect as setting the property IncludeInCurrentNavigation of your PublishingPage to false. Using ExcludeFromNavigation, however is faster, as documented on MSDN. Another advantage is that you do not have to worry about the checked out state and publishing state of your page. If you change the property of you PublishingPage, you need to call Update(). If you have versioning switched on, you first need to check out the page. After modifiying the page options, you need to check-in and re-publish the page. And you need to take into account that a page can already be checked out. So using ExcludeFromNavigation is much easier.
The strange thing here that confused me is that your PublishingWeb also has a collection of navigation nodes in the property CurrentNavigationNodes. If you never touched any of the navigation options in the user interface, this collection is, and remains, empty when you hide pages from code. If you hide a page from the navigation using the user interface, SharePoint adds a SPNavigationNode object to the CurrentNavigationNodes for each page.
Each navigation node has a property IsVisible and it seemed logical to me that this is used when you hide a page from the navigation. Well, it is not. It also sets the IncludeInCurrentNavigation property of the publishing page. My advise is not to worry about these navigation nodes, let SharePoint take care of that.
Show a page in the navigation
If you want to include a page in the navigation, you use the IncludeInNavigation method of the PublishingWeb. The first boolean parameter adds your page back to the current navigation. If you pass a true, it will be added back to the global nav.
pw.IncludeInNavigation(false, page.ListItem.UniqueId);
pw.Update();
If pages are not included in the navigation (you can check this using the PublishingWeb property IncludePagesInNavigation, you need to add a new SPNavigationNode to the navigation nodes:
bool found = false;
foreach (SPNavigationNode navNode in pw.CurrentNavigationNodes)
{
if (navNode.Url.ToLower().EndsWith(page.Url.ToLower()))
{
found = true;
break;
}
}
if (!found)
{
SPNavigationNode newNode = new SPNavigationNode(page.Title, string.Format("{0}/{1}", web.ServerRelativeUrl, page.Url), false);
pw.CurrentNavigationNodes.AddAsLast(newNode);
pw.Update();
}
Please note that you now have a custom navigation node in the navigation options, which is different from the default way of using pages in the navigation:
Although the user interface does not show the navigation nodes for your pages, they still are present (if they were there because you manually changed something in the user interface). If IncludePagesInNavigation is false, you are never able to show the node for your page. The drawback of adding a new node in this case is that when you switch on pages in the navigation, you will end up with 2 navigation nodes to your page.
Hope this makes playing with the navigation options of pages in MOSS a bit clearer. In this blogpost, I wrote about other navigation options.
If you now switch