DooDoo
DooDoo

Reputation: 13447

How to create multi-level menu using data stored in database?

I have some hierarchical data that I want build a menu on my site on SharePoint. How I can build a menu using this data on SharePoint.

Can any one show me a sample that display database data on master page in SharePoint.

Upvotes: 1

Views: 668

Answers (1)

Dalorzo
Dalorzo

Reputation: 20014

The "Sharepoint way" to do that is by creating a custom navigation provider. From Microsoft site here is the sample:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Publishing.Navigation;
using System.Web;
using System.Web.UI.WebControls;
using System.Configuration;

namespace MyCustomNav
{
public class Navigation: PortalSiteMapProvider
{
public override SiteMapNodeCollection GetChildNodes(System.Web.SiteMapNode 
node)
{
PortalSiteMapNode pNode = node as PortalSiteMapNode;
if (pNode != null)
{
if (pNode.Type == NodeTypes.Area)
{
// TODO: Make your database call here and create the node based on your query..

SiteMapNodeCollection nodeColl = base.GetChildNodes(pNode);
SiteMapNode childNode = new SiteMapNode(this, 
"<http://www.microsoft.com>", "<http://www.microsoft.com>", "Microsoft");

SiteMapNode childNode1 = new SiteMapNode(this, 
"<http://support.microsoft.com>", "<http://support.microsoft.com>", "Support");

nodeColl.Add(childNode);

SiteMapNodeCollection test = new SiteMapNodeCollection();
test.Add(childNode1);
childNode.ChildNodes = test;

return nodeColl;
}
else
return base.GetChildNodes(pNode);
}
else
return new SiteMapNodeCollection();
}
}
}

This goes to you web.config

<add name="MyCustomNavigationProvider" type="MyCustomNav.Navigation, MyCustomNav" 
NavigationType="Current" />

This the asp control

<SharePoint:AspMenu
ID="TopNavigationMenu"
  Runat="server"
  DataSourceID="topSiteMap1"
  EnableViewState="false"
  AccessKey="<%$Resources:wss,navigation_accesskey%>"
  Orientation="Horizontal"
  StaticDisplayLevels="1"
  MaximumDynamicDisplayLevels="3"
  DynamicHorizontalOffset="0"
  StaticPopoutImageUrl="/_layouts/images/menudark.gif"
  StaticPopoutImageTextFormatString=""
  DynamicHoverStyle-BackColor="#CBE3F0"
  SkipLinkText=""
  StaticSubMenuIndent="0"
  CssClass="ms-topNavContainer">
<StaticMenuStyle/>
<StaticMenuItemStyle CssClass="ms-topnav" ItemSpacing="0px"/>
<StaticSelectedStyle CssClass="ms-topnavselected" />
<StaticHoverStyle CssClass="ms-topNavHover" />
<DynamicMenuStyle BackColor="#F2F3F4" BorderColor="#A7B4CE" 
  BorderWidth="1px"/>
<DynamicMenuItemStyle CssClass="ms-topNavFlyOuts"/>
<DynamicHoverStyle CssClass="ms-topNavFlyOutsHover"/>
<DynamicSelectedStyle CssClass="ms-topNavFlyOutsSelected"/>
</SharePoint:AspMenu>
<asp:SiteMapDataSource
  ShowStartingNode="False"
  SiteMapProvider="MyCustomNavigationProvider"
  id="topSiteMap1"
  runat="server"
  StartFromCurrentNode="true"/>

Full article here:

http://msdn.microsoft.com/en-us/library/cc789625%28v=office.14%29.aspx

Upvotes: 1

Related Questions