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.
Weblog Ton Stegeman [MVP] > Posts > Navigation options in a SharePoint Publishing Site
Navigation options in a SharePoint Publishing Site

As with almost anything in SharePoint 2007, it is possible to set the navigation options for a publishing site in code. To be able to do this, you need to reference the Microsoft.SharePoint.Publishing assembly and add these using directives.

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Publishing;
    using Microsoft.SharePoint.Navigation;

Initialize

The following examples all use this initialization snippet:

    SPSite site = new SPSite("http://office2007:300");
    SPWeb web = site.AllWebs["intro/webcms"];
    string pagename = "IntroductieCMS.aspx";
 
    PublishingWeb pw = Microsoft.SharePoint.Publishing.PublishingWeb.GetPublishingWeb(web);
    PublishingPageCollection webpages = pw.GetPublishingPages();

Example 1 – Switch “Show Pages” on

This example switches the “Show Pages” option for a publishing site on. This setting is found in SharePoint on the Modify Navigation page.
navigation1

    if (!pw.IncludePagesInNavigation)
    {
        pw.IncludePagesInNavigation = true;
        pw.Update();
    }

Example 2 – Exclude a page from the navigation

This sample excludes the page called “IntroductieCMS.aspx” from the navigation. This is a normal page in the Pages document library.
navigation2

    PublishingPage page = webpages[string.Format("Pages/{0}", pagename)];
    if ((page != null) && (page.IncludeInCurrentNavigation))
    {
        page.CheckOut();
        page.IncludeInCurrentNavigation = false;
        page.Update();
        page.CheckIn("changed the navigation options");
    }

In this sample the page is in a Pages library that has versioning with the Require Check Out switched on. There we need to to a checkout of the item, before we can change it.

Example 3 – Add a link to the navigation

This example adds a custom link to the current navigation. This is the navigation on the left side of the page.
navigation3

    SPNavigationNodeCollection navNodes = pw.CurrentNavigationNodes;
    SPNavigationNode newNavNode = new SPNavigationNode("e-office", "http://www.e-office.com", true);
    navNodes.AddAsLast(newNavNode);
    newNavNode.Properties.Add("NodeType", NodeTypes.AuthoredLinkPlain.ToString());
    newNavNode.Update();

By using the NodeType property, you can choose the type of link. You can also use this to add a heading to the navigation. Please make sure that you first add the node to the collection and the set the properties in the HashTable. Otherwise the Properties value will be null and your code will fail.
To add a link to the navigation on top of the page, use the GlobalNavigationNodes property of the PublishingWeb.

These are just a few very basic examples, but they should be enough to get you started.

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

 Links

  SharePoint Object on CodePlex
  Screencast introducing SharePoint Objects
  Content by Type and Filter Web Parts on CodePlex
  Archive
  Archive (Calendar)

 My Latest Blog Posts

Scripting SharePoint 2007 setup: choices and conceptsUse SHIFT+ENTER to open the menu (new window).
Adventures in Visual Studio 2010: Migrate the Content By Type web part to SharePoint 2010Use SHIFT+ENTER to open the menu (new window).
Register SharePoint themes by using a featureUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010 development on Windows 2008 Server R2 – Getting StartedUse SHIFT+ENTER to open the menu (new window).
New release SharePoint Objects: features and groupsUse SHIFT+ENTER to open the menu (new window).
Constructing the url to the SharePoint Edit Permissions pageUse SHIFT+ENTER to open the menu (new window).
Screencast: introduction to SharePoint ObjectsUse SHIFT+ENTER to open the menu (new window).
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).