Friends, Technology, Web2.0 - What I am reading

    [Home] [Recent] [Site Map]

   

HOW TO: Programmatically customize site navigation in MOSS 2007 - SharePoint Blogs

In the previous version of SharePoint the QuickLaunch menu was controlled via the ONET.XML file and ASPX pages that made up a Site Definition.  Making changes to the QuickLaunch menu was a cumbersome task at best.

 

The new version of SharePoint provides significant improvements to the navigational elements within a SharePoint site as well as the ability to change these elements programmatically!  This article describes how to programmatically interact with the navigation elements in order to customize SharePoint site navigation.  This article also investigates the security trimming capabilities of the SharePoint UI with respect to programmatically edited navigation links.

 

Controlling navigation via the SPWeb object!

 

The SPWeb object has been enhanced in MOSS 2007 to support the ability to control the navigation of a site programmatically!  Let’s examine how this can be done.

 

The SPWeb object has a new property named Navigation that returns a SPNavigation object.  This object allows developers to programmatically control the navigation of a SharePoint site.

 

Shared Navigation Settings

 

One of the new settings you may apply to a site’s navigation structure is whether or not the site uses the navigation menus from its parent site.

 

These examples assume you have created a Site Collection named test in the Site Directory and a sub site named sharednav under the test Site Collection as well as a sub site named nosharednav under the test Site Collection.

 

Once these sites have been created their URLs will look like this:

 

http://SharePointServer/SiteDirectory/test

http://SharePointServer/SiteDirectory/sharednav

http://SharePointServer/SiteDirectory/nosharednav

 

Here is an example of how to set a sub site to use the navigation from its parent site. 

 

SPSite sharedNavSite = new SPSite(“http://SharePointServer/SiteDirectory/test/sharednav”);

SPWeb sharedNavWeb = sharedNavSite.OpenWeb();

sharedNavWeb.Navigation.UseShared = true;

 

The navigation for the sharednav site looks like this after the lines of code above have been run to set the property.  Notice the breadcrumb trail at the top of the page uses the breadcrumb trail for the test Site Collection and the horizontal menu bar uses the items from the test Site Collection as well.


 

Here is an example of how to set a sub site not to use the navigation from a parent’s site. 

 

SPSite noSharedNavSite = new SPSite(“http://SharePointServer/SiteDirectory/test/nosharednav”);

SPWeb noSharedNavWeb = noSharedNavSite.OpenWeb();

noSharedNavWeb.Navigation.UseShared = false

 

The navigation for the nosharednav site looks like this after the lines of code above have been run to set the property.  Notice the breadcrumb trail at the top of the page uses the breadcrumb trail for the nosharednav sub site and the horizontal menu bar uses the items from the nosharednav sub site.

 

 

QuickLaunch Menu Items

 

The QuickLaunch navigation menu may also be accessed programmatically.  You can create new menu items in the QuickLaunch navigation menu and remove them.  You can also specify if the link is external to the site.

 

Here is how you add a menu item to the QuickLaunch navigation menu. 

 

These QuickLaunch examples assume you have created a Site Collection named quicklaunch.

 

Once this Site Collection has been created its URL will look like this:

 

http://SharePointServer/SiteDirectory/quicklaunch

 

In this example we will add two links to the QuickLaunch menu for the quicklaunch Site Collection, one link will be internal and one will be external.  The internal link will point to the Links list in the QuickLaunch site.  The external link will point to the SharePoint Experts web site.

 

SPSite quickLaunchSite = new SPSite(“http://SharePointServer/SiteDirectory/quicklaunch”);

SPWeb quickLaunchWeb = quickLaunchSite.OpenWeb();

 

SPNavigationNodeCollection quickLaunchNodes = quickLaunchWeb.Navigation.QuickLaunch;

 

SPNavigationNode internalMenuItem = new SPNavigationNode(“Links”, “Lists/Links/AllItems.aspx”, false);

quickLaunchNodes.AddAsFirst(internalMenuItem);

 

SPNavigationNode externalMenuItem = new SPNavigationNode(“SharePoint Experts”, “http://www.SharePointExperts.com”, true);

quickLaunchNodes.AddAsFirst(externalMenuItem);

 

quickLaunchWeb.Update();

 

*Note:  If you do not call the Update() method on the SPWeb object you are working with, you will need to reset IIS to see your changes.

 

Here is what the quicklaunch Site Collection looks like after the above code has been executed:

 

Here is how you remove a menu item from the QuickLaunch navigation menu. 

 

In this example we will remove the external link to the SharePoint Experts web site.

 

SPSite quickLaunchSite = new SPSite(“http://SharePointServer/SiteDirectory/quicklaunch”);

SPWeb quickLaunchWeb = quickLaunchSite.OpenWeb();

 

SPNavigationNodeCollection quickLaunchNodes = quickLaunchWeb.Navigation.QuickLaunch;

 

quickLaunchNodes.Delete(quickLaunchNodes([0]);

quickLaunchWeb.Update();

 

Here is what the quicklaunch Site Collection looks like after the above code has been executed:

 

 

Taking the QuickLaunch menu one step further

 

The QuickLaunch menu is capable of displaying links in a grouped fashion, as you see in the screenshot above.  For example, the Lists menu item in the screenshot above has two items under it – the Calendar and Tasks links.  Creating your own grouped links can be done programmatically.  This is once again a simple process that requires very little code.

 

Here is how you add grouped menu items to the QuickLaunch navigation menu. 

 

In this example we will create a header link for the group of links and name it Administration.  Then we will add two links under the Administration header link.  The links will link to the Create and Site Settings pages for the quicklaunch Site Collection.

 

SPSite quickLaunchSite = new SPSite(“http://SharePointServer/SiteDirectory/quicklaunch”);

SPWeb quickLaunchWeb = quickLaunchSite.OpenWeb();

SPNavigationNodeCollection quickLaunchNodes = quickLaunchWeb.Navigation.QuickLaunch;

 

SPNavigationNode internalMenuItem = new SPNavigationNode("Administration", "", false);

quickLaunchNodes.AddAsFirst(internalMenuItem);

 

SPNavigationNode internalSubMenuItem1 = new SPNavigationNode("Site Settings", "_layouts/settings.aspx", false);

quickLaunchNodes[0].Children.AddAsFirst(internalSubMenuItem1);

 

 SPNavigationNode internalSubMenuItem2 = new SPNavigationNode("Create", "_layouts/create.aspx", false);

quickLaunchNodes[0].Children.AddAsFirst(internalSubMenuItem2);

 

quickLaunchWeb.Update();

  

Here is what the quicklaunch Site Collection looks like after the above code has been executed:

 

 

*Note:  I added the Create and Site Settings links to the QuickLaunch bar to test the security trimmed aspects of the SharePoint UI.  I wanted to see if SharePoint would hide these links from users who did not have access to see them because MOSS 2007 hides these links in the out of the box SharePoint UI from users who only have reader access.

 

I created a user named reader and granted the user read only access to the quicklaunch Site Collection.  When I logged into the quicklaunch Site Collection with this user the links I just added to the QuickLaunch were available to the reader user!  This is an important thing to note, and should be kept in mind when adding links to the QuickLaunch menu!

 

Here is what the quicklaunch Site Collection looked like when the reader user logged in:

 

  

Here is the screen you see when you click one of the links you do not have access to:

 

  

Top Navigation Menu Items

 

The top navigation menu may also be accessed programmatically.  You can create new menu items in the top navigation menu and remove them.  You can also specify if the link is external to the site.

 

Here is how you add a menu item to the top navigation menu. 

 

This example assumes you have created a Site Collection named topnavigation.

 

Once this Site Collection has been created its URL will look like this:

 

http://SharePointServer/SiteDirectory/topnavigation

 

In this example we will add two links to the top navigation menu for the topnavigation Site Collection, one link will be internal and one will be external.  The internal link will point to the Links list in the topnavigation site.  The external link will point to the SharePoint Experts web site.

 

SPSite topNavigationSite = new SPSite(“http://SharePointServer/SiteDirectory/topnavigation”);

SPWeb topNavigationWeb = topNavigationLaunchSite.OpenWeb();

 

SPNavigationNodeCollection topNavigationNodes = topNavigationWeb.Navigation.TopNavigationBar;

 

SPNavigationNode internalMenuItem = new SPNavigationNode(“Links”, “Lists/Links/AllItems.aspx”, false);

topNavigationNodes.AddAsFirst(internalMenuItem);

 

SPNavigationNode externalMenuItem = new SPNavigationNode(“SharePoint Experts”, “http://www.SharePointExperts.com”, true);

topNavigationNodes.AddAsFirst(externalMenuItem);

 

topNavigationWeb.Update();

 

Here is what the topnavigation Site Collection looks like after the above code has been executed:

 

  

Here is how you delete a menu item from the top navigation menu.  In this example we will remove the Links link we just added to the top navigation menu for the topnavigation Site Collection.

 

SPSite topNavigationSite = new SPSite(“http://SharePointServer/SiteDirectory/topnavigation”);

SPWeb topNavigationWeb = topNavigationLaunchSite.OpenWeb();

 

SPNavigationNodeCollection topNavigationNodes = topNavigationWeb.Navigation.TopNavigationBar;

 

topNavigationNodes.Delete(topNavigationNodes[0]);

topNavigationWeb.Update();

 

Here is what the topnavigation Site Collection looks like after the above code has been executed:

 

 

Taking the Top Navigation one step further

 

The top Navigation menu is capable of displaying dropdown menus that consist of multiple items.  Adding additional sub menu items under the topmost item is a simple process and requires very little code.

 

Here is how you add a sub menu item to a top level menu item in the top navigation menu.  In this example we will add a new top level menu item to the top navigation menu that has two sub menu items under it for the topnavigation Site Collection.

 

SPSite topNavigationSite = new SPSite(“http://SharePointServer/SiteDirectory/topnavigation”);

SPWeb topNavigationWeb = topNavigationLaunchSite.OpenWeb();

SPNavigationNodeCollection topNavigationBarNodes =

 topNavigationWeb.Navigation.TopNavigationBar;

 

SPNavigationNode headerMenuItem = new SPNavigationNode("SharePoint Sites", "", false);

topNavigationBarNodes.AddAsFirst(headerMenuItem);

 

SPNavigationNode externalSubMenuItem1 = new SPNavigationNode("SharePoint Experts", "http://www.SharePointExperts.com", true);

topNavigationBarNodes[0].Children.AddAsFirst(externalSubMenuItem1);

 

SPNavigationNode externalSubMenuItem2 = new SPNavigationNode("SharePoint University", "http://www.SharePointU.com", true);

topNavigationBarNodes[0].Children.AddAsFirst(externalSubMenuItem2);

 

topNavigationWeb.Update();

 

Here is what the topnavigation Site Collection looks like after the above code has been executed:

 

 

*Note:  I was curious to see if the Top Navigation menu reacted the same way the QuickLaunch menu bar did regarding the security trimming of the UI.  So I created an Administration Header link and two sub links for the Create and Site Settings pages. 

 

I granted the reader user read only access to the topnavigation Site Collection and when I logged into the topnavigation Site Collection with this user the links I just added to the Top Navigation menu were available to the reader user!  This is an important thing to note, and should be kept in mind when adding links to the Top Navigation menu!

 

Here is the code I used to add the links:

 

SPSite topNavigationSite = new SPSite(“http://SharePointServer/SiteDirectory/topnavigation”);

SPWeb topNavigationWeb = topNavigationLaunchSite.OpenWeb();

SPNavigationNodeCollection topNavigationBarNodes =

 topNavigationWeb.Navigation.TopNavigationBar;

 

SPNavigationNode headerMenuItem = new SPNavigationNode("Administration", "", false);

topNavigationBarNodes.AddAsFirst(headerMenuItem);

 

SPNavigationNode internalSubMenuItem1 = new SPNavigationNode("Site Settings", "_layouts/settings.aspx", false);

topNavigationBarNodes[0].Children.AddAsFirst(internalSubMenuItem1);

 

SPNavigationNode internalSubMenuItem2 = new SPNavigationNode("Create", "_layouts/create.aspx", false);

 topNavigationBarNodes[0].Children.AddAsFirst(internalSubMenuItem2);

 

topNavigationWeb.Update();

 

Here is what the topnavigation Site Collection looked like when the reader user logged in:

 

Note:  I attempted to add sub menu items to sub menu items in the top navigation menu.  The object model did not put any rows into the database when I did this.  I decided to manually add a row to the NavNodes table in the database to see if the menu would render.  It did not, so I assume the code that generates the UI for the top navigationmenu does not look for, or generate sub menu items more than one level deep.


>>Source Link

Related Posts:
Xbox Live_s Major Nelson
Xbox 360 & SharePoint 2007 Weblog
Carsten Keutmann_s Blog
Mohamed Zaki_s Blog [Sharepoint MVP]
The Mit_s Blog
Mart Muller_s Sharepoint Weblog
Microsoft SharePoint Products and Technologies Team Blog
SharePoint Solutions Blog
4GuysFromRolla.com Headlines
ASP.NET Blogs
SharePoint Blogs
SharePoint Blogs
Joel on Software
ADO Guy_s Rants and Raves
Microsoft Live Labs
GadgetNews



Month Archives:
Jan 2007
Dec 2006

Top Tags:
Sharepoint .NET ASP.NET Community News MOSS 2007 SharePoint 2007 Marketplace Office 2007 AJAX Fun WSS 3.0 Atlas Announcements Administration Système General Software Development


@2007 All rights Reserved