in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Ton Stegeman [MVP] weblog

I have moved my blog to http://www.tonstegeman.com/blog. If you have a blogpost at sharepointblogs that does not display all the content on the right hand side, please go to the new blog. It has all the posts that are on this blog as well. I you have any feedback, please send me a message through the contact form.

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

No Comments

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts