Share via


Adding Links through the Object Model

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Windows SharePoint Services uses standard ASP.NET controls for navigation. For example, the System.Web.SiteMapNode class is implemented for the global breadcrumbs appearing at the top left of SharePoint pages. The Menu control is used for both top navigation links and links in Quick Launch, and the navigation hierarchy of SharePoint sites is exposed through the System.Web.SiteMapProvider class. Custom navigation controls can be bound to the provider, including Treeview, Menu, and SiteMapPath controls.

You can use types and members of the Microsoft.SharePoint.Navigation namespace to customize the navigation of sites. You can use the Windows SharePoint Services object model to manage both the Quick Launch area and the top navigation area, reordering links, adding new links, or removing links. You can also manage site inheritance in relation to parent sites.

The Navigation property of the Microsoft.SharePoint.SPWeb class gets a Microsoft.SharePoint.Navigation.SPNavigation object that contains all the navigation properties for a specified site, including its navigation inheritance status relative to the parent site, as well as page hierarchy and navigation node collections. The QuickLaunch property of the SPNavigation object returns the collection of navigation nodes found in the Quick Launch area, the Web property gets the top-level root site of the parent site collection, and the TopNavigationBar property returns the collection of navigation nodes that represent links in the top navigation area. An SPNavigationNodeCollection object represents a collection of the ordered navigation nodes for a given object and provides the ability to add or remove nodes programmatically.

Note   Although a SPNavigationNode object can contain arbitrary URLs, link fix-up cannot be implemented on the object for these URLs. However, the object can contain documents and pages in the site collection, and link fix-up is supported.

Example

The following example creates a link and adds it to the top of the Quick Launch bar:

Dim siteCollection As SPSite = SPControl.GetContextSite(Context)
Dim site As SPWeb = siteCollection.AllWebs("TestSite")
Dim subSite As SPWeb = site.Webs("SubTestSite")

Dim nodes As SPNavigationNodeCollection = subSite.Navigation.QuickLaunch
Dim navNode As New SPNavigationNode("New Link", "http://www.msn.com", True)
nodes.AddAsFirst(navNode)
SPSite siteCollection = SPControl.GetContextSite(Context);
SPWeb site = siteCollection.AllWebs["TestSite"];
SPWeb subSite = site.Webs["SubTestSite"];

SPNavigationNodeCollection nodes = subSite.Navigation.QuickLaunch;
SPNavigationNode navNode = new SPNavigationNode("New Link", "http://www.msn.com", true);
nodes.AddAsFirst(navNode);

The third parameter of the SPNavigationNode constructor is set to true because the URL for the new link is external to the SharePoint deployment.

To run this example, you must add a Microsoft.SharePoint.WebControls.FormDigest control to the page making the post. For information on how to add a FormDigest control, see Security Validation and Making Posts to Update Data. The example also requires referencing and importing the Microsoft.SharePoint, Microsoft.SharePoint.Navigation and Microsoft.SharePoint.WebControls namespaces. For basic information about how to create a Web application that runs in the context of Windows SharePoint Services, see How to: Create a Web Application in a SharePoint Web Site.

See Also

Tasks

How to: Customize the Display of Quick Launch

Concepts

Custom Navigation and New User Interface Elements

Customizing Quick Launch and the Top Link Bar Through the User Interface

How to: Share the Top Link Bar Between Sites

Using a Custom Data Source for Navigation