Introduction
This article explains how to use the ASP.NET TreeView
for the current navigation in SharePoint (WSS 3/MOSS 2007).
This is useful if you use SharePoint as a Content Management System, for example, if you have a publishing site with many pages sorted in a content hierarchy that you wish to present in a user-friendly treeview navigation control.
Naturally, you could benefit with combining the treeview control with a horizontal menu control on your master page so that the primary, global navigation is presented by a horizontal menu on the top of your page, and the secondary, local navigation is presented by a vertical tree on the left side of your site.
Background
Out-of-the box SharePoint uses the SharePoint:AspMenu
for the current navigation, usually shown on the left side of a page.
SharePoint also shpis with a SharePoint:SPTreeView
control that shows the categories of a site. However, if you want to render the current navigation in the left side as a tree, with expandable and collapsible menu items, you have to do a few changes to your master page and your webconfig.sps.xml file.
Using the Code
- If you have a content placeholder with the ID "
PlaceHolderLeftNavBar
", then locate the SharePoint:AspMenu
within the placeholder and replace it with ASP:TreeView
. If you do not have a placeholder, then insert the control on your master page.
- As the data source for the
TreeView
control, use SiteMapProvider="CurrentNavSiteMapProviderNoEncode"
. If you already have a "PlaceHolderLeftNavBar
" content placeholder, then you can simply change the SiteMapProvider="CurrentNavSiteMapProvider"
beneath the SharePoint:AspMenu
replaced in step 1.
Here, I use the PortalSiteMap data source:
<ASP:TreeView id="MyID" runat="server" DataSourceID="MyDatasource"
ExpandDepth="0"></ASP:TreeView>
<PublishingNavigation:PortalSiteMapDataSource ID="MyDatasource"
runat="server" SiteMapProvider="CurrentSiteMapProviderNoEncode"
- Finally, you need to change your webconfig.sps.xml files, which you should find here: "C:\\Progam Files\Common Files\Microsoft Shared\web server extensions\12\CONFIG".
Locate the provider named "CurrentNavSiteMapProviderNoEncode
" and add the attribute RequireUniqueKeysForNodes="true"
.
The following shows the relevant section:
<add path="configuration/system.web/siteMap/providers"
id="{009E5494-26C5-4181-936F-4D16F444B642}">
<add name="GlobalNavSiteMapProvider"
description="CMS provider for Global navigation"
type="Microsoft.SharePoint.Publishing.Navigation.PortalSiteMapProvider,
Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c" NavigationType="Global" EncodeOutput="true"/>
<add name="CombinedNavSiteMapProvider"
description="CMS provider for Combined navigation"
type="Microsoft.SharePoint.Publishing.Navigation.PortalSiteMapProvider,
Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c" NavigationType="Combined" EncodeOutput="true"/>
<add name="CurrentNavSiteMapProvider"
description="CMS provider for Current navigation"
type="Microsoft.SharePoint.Publishing.Navigation.PortalSiteMapProvider,
Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c" NavigationType="Current" EncodeOutput="true"/>
<add name="CurrentNavSiteMapProviderNoEncode"
description="CMS provider for Current navigation, no encoding of output"
type="Microsoft.SharePoint.Publishing.Navigation.PortalSiteMapProvider,
Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c" NavigationType="Current" EncodeOutput="false"
RequireUniqueKeysForNodes="true"/>
<add name="SiteDirectoryCategoryProvider"
description="Site Directory category provider"
type="Microsoft.SharePoint.Portal.WebControls.SiteDirectoryCategoryProvider,
Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c"/>
<add name="MySiteMapProvider"
description=
"MySite provider that returns areas and based on the current user context"
type="Microsoft.SharePoint.Portal.MySiteMapProvider,
Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c"/>
<add name="MySiteLeftNavProvider"
description=
"MySite Left Nav provider that returns areas
and based on the current user context"
type="Microsoft.SharePoint.Portal.MySiteLeftNavProvider,
Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c"/>
</add>
- Save your files and reset IIS.
Points of Interest
There are various properties that you can use to customize the TreeView
control.