Customizing Navigation Controls and Providers

To customize navigation, you should rely on the ASP.NET Site Navigation mechanism, because it provides a standard, consistent, and easily managed solution. For more information, see the Microsoft ASP.NET Developer Center on MSDN.

Customizing Web Display Controls (Menu, TreeView, and SiteMapPath)

You can customize the look and feel of standard display controls by using cascading style sheets (CSSs). You can also write your own ASP.NET 2.0 controls and use them in Office SharePoint Server 2007 navigation. To learn more, see the System.Web.UI.WebControls.Menu class documentation.

Note Note:

Office SharePoint Server 2007 also provides default navigation components that you can customize, such as the Content By Query Web Part and the Table of Contents Web Part. You can configure these two controls to present a custom selection of links.

The System.Web.UI.WebControls.TreeView and System.Web.UI.WebControls.Menu navigation controls bind to ASP.NET 2.0 data sources, providing an abstract layer between the Web navigation controls and the underlying navigation provider.

Implementing Site Map Providers

To implement your own site map provider, you can derive a custom provider class from any of the default site map providers. Office SharePoint Server 2007 includes several default providers. You can also derive a custom provider class from the >SiteMapProvider class in the System.Web namespace. However, deriving the Office SharePoint Server 2007 site map providers supplies several additional benefits such as navigation node caching and security trimming. Therefore, you should use the Office SharePoint Server 2007 caching and security trimming infrastructure instead of writing your own caching and security trimming in a custom provider by deriving from the default providers. Although the site map provider classes have approximately twenty abstract or virtual methods, only a small number must be overridden and implemented in a custom site map provider. The following code example derives from the PortalSiteMapProvider class and demonstrates how to add items to the site map provider.

C#
public class CustomNavProviderWithList : PortalSiteMapProvider
    {
        public override SiteMapNodeCollection GetChildNodes(System.Web.SiteMapNode node)
        {
            PortalSiteMapNode portalNode = node as PortalSiteMapNode;
            if (portalNode != null)
            {
                if (portalNode.Type == NodeTypes.Area)
                {
                 
                    SiteMapNodeCollection nodeColl = base.GetChildNodes(portalNode);
                    using (SPSite currentSite = new SPSite(portalNode.Url))
                     {
                       SPWeb currentWeb = currentSite.OpenWeb();
                       SPListCollection lists = currentWeb.Lists;
                       foreach (SPList list in lists)
                       {
                           string listUrl = list.DefaultViewUrl;
                           PortalSiteMapNode childNode = new PortalSiteMapNode(webNode, listUrl, NodeTypes.custom, list.Title, listUrl, null);
                           nodeColl.Add(childNode);
                    }
                  }
                  return nodeColl;
                }
                else
                {
                    return base.GetChildNodes(portalNode);
                }
            }
            else
            {
                return new SiteMapNodeCollection();
            }
        }
    }

See Also

Tags :


Community Content

HJohns
There are 2 errors in the example.
PortalSiteMapNode childNode = new PortalSiteMapNode(portalNode.WebNode, listUrl, NodeTypes.Custom, list.Title, listUrl, null);
Tags : contentbug

RenaudB
Warning, this code does not clean'up SPWeb object

see following article:

http://msdn2.microsoft.com/en-us/library/aa973248.aspx

Tags :

Colin McGraw
more elegant (and works) code
public override SiteMapNodeCollection GetChildNodes(System.Web.SiteMapNode node)
{
PortalSiteMapNode portalNode = node as PortalSiteMapNode;
if (portalNode != null)
{
//Colin's edit:
//we only want to add our new node once at the root instead of every
//time we encounter an "Area" node.
if (portalNode == RootNode)
{
SiteMapNodeCollection nodeColl = base.GetChildNodes(portalNode);
SPWeb currentWeb = SPContext.Current.Web;
SPListCollection lists = currentWeb.Lists;
PortalSiteMapNode rootNode = new PortalSiteMapNode(
portalNode.WebNode,
"listsNode",
NodeTypes.Heading,
"",
"Lists",
null);
nodeColl.Add(rootNode);
int i = 0;
foreach (SPList list in lists)
{
if (i > 10)
break;
i++;
string listUrl = list.DefaultViewUrl;
PortalSiteMapNode childNode = new PortalSiteMapNode(
rootNode.WebNode,
listUrl,
NodeTypes.Custom,
listUrl,
list.Title,
null);
rootNode.ChildNodes.Add(childNode);
}
                    return nodeColl;
}
else
{
return base.GetChildNodes(portalNode);
}
}
else
{
return new SiteMapNodeCollection();
}
}
  
  

note that in my code you dont need to dispose of the SPWeb object, since you are getting it from the context and if you dispose of it, it will not be availalble to other components on the page.


Page view tracker