Skip to main content

Weblog Ton Stegeman [MVP]

Go Search
Home
  

ODC 2008
If you have a question or suggestion, please contact me through Windows Live Messenger.
My status: .

If I am not online, please send me an e-mail.
Other Blogs
There are no items in this list.
Weblog Ton Stegeman [MVP] > Posts > Showing and hiding Publishing pages in MOSS navigation
Showing and hiding Publishing pages in MOSS navigation

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:

image

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.

image

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:

image

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

Comments

There are no comments yet for this post.
Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Your city *


Type the name of the city you live in (making it easier to handle spam...)

CurrentDate *

Select the current date (see if this gives me fewer spam...)
Attachments

 My Latest Blog Posts

SharePoint 2007 and Reporting ServicesUse SHIFT+ENTER to open the menu (new window).
SharePoint Objects – Insight in usage of your SharePoint artifactsUse SHIFT+ENTER to open the menu (new window).
SharePoint 2007 Custom list schema and the Content Query Web PartUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010 Silverlight Client Object Model – ExecuteQuery vs ExecuteQueryAsyncUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010, the Client Object Models and Bing MapsUse SHIFT+ENTER to open the menu (new window).
Having fun with SharePoint 2010, Silverlight 3 and Bing MapsUse SHIFT+ENTER to open the menu (new window).
Connecting TFS 2010 projects to SharePoint sitesUse SHIFT+ENTER to open the menu (new window).
Adding a database to the SharePoint database Server using SPDatabaseUse SHIFT+ENTER to open the menu (new window).
New version of Content by Type and filter web parts and a new family memberUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010 Client Object Models – The ECMAScript libraryUse SHIFT+ENTER to open the menu (new window).
Slides and code of the DIWUG presentation of 29 september 2009Use SHIFT+ENTER to open the menu (new window).
Introducing SharePoint Security BlueprintsUse SHIFT+ENTER to open the menu (new window).
JQuery and SharePoint – Lookup fields and event listsUse SHIFT+ENTER to open the menu (new window).
SharePoint Filter web parts: using a context filter in a page layoutUse SHIFT+ENTER to open the menu (new window).
DevDays 2009: Ask the expertsUse SHIFT+ENTER to open the menu (new window).