sharepoint2010 自定义导航栏
此处获取的是顶部的导航栏
sahrepoint2010使用sitemap来存储导航栏,因此使用SiteMapProvider即可获取,但需要注意的是,获取的导航栏需要现在网站设置-导航里面去设置导航栏以及是否显示2级目录等。
此处我生成的是Smooth的html,由于我只需要显示2级,所以没有迭代,你也可以使用迭代生成多级目录,自定义导航栏
用户空间做好后,在desinger里面将原来显示导航的隐藏掉,一般在这个网站的v4.master,在添加自定义的用户空间即可
需要隐藏的对应代码,设置Visible="false"
<asp:ContentPlaceHolder id="PlaceHolderHorizontalNav" runat="server" Visible="false">
<SharePoint:AspMenu
ID="TopNavigationMenuV4"
Runat="server"
EnableViewState="false"
DataSourceID="topSiteMap"
AccessKey="<%$Resources:wss,navigation_accesskey%>"
UseSimpleRendering="true"
UseSeparateCss="false"
Orientation="Horizontal"
StaticDisplayLevels="2"
MaximumDynamicDisplayLevels="1"
SkipLinkText=""
CssClass="s4-tn"/>
<SharePoint:AspMenu
ID="TopNavigationMenuV4"
Runat="server"
EnableViewState="false"
DataSourceID="topSiteMap"
AccessKey="<%$Resources:wss,navigation_accesskey%>"
UseSimpleRendering="true"
UseSeparateCss="false"
Orientation="Horizontal"
StaticDisplayLevels="2"
MaximumDynamicDisplayLevels="1"
SkipLinkText=""
CssClass="s4-tn"/>
需要添加的用户控件
<%@ Register TagPrefix="wssuc" TagName="SmoothNavMenu" src="~/_controltemplates/ECISystem/SmoothNavMenu.ascx" %>
<wssuc:SmoothNavMenu runat="server" id="SmoothNavMenu"></wssuc:SmoothNavMenu>
<wssuc:SmoothNavMenu runat="server" id="SmoothNavMenu"></wssuc:SmoothNavMenu>
具体获取html代码
1 /// <summary>
2 /// 获取站点导航
3 /// </summary>
4 /// <returns></returns>
5 private SiteMapNode GetSiteMapRootNodeOfCurrentWeb()
6 {
7 SiteMapProvider smp = PortalSiteMapProvider.CombinedNavSiteMapProvider;
8 SiteMapNode rootMap = smp.RootNode;
9 return rootMap;
10 }
11 /// <summary>
12 /// get menu html
13 /// </summary>
14 /// <returns></returns>
15 public string GetMenuString()
16 {
17 StringBuilder sbMenu = new StringBuilder();
18 SiteMapNode smn = GetSiteMapRootNodeOfCurrentWeb();
19 sbMenu.Append("<li><a href=\"/\">首页</a>");
20 foreach (SiteMapNode node in smn.ChildNodes)
21 {
22 string strUrl = node.Url.ToString();
23 string strTitle = node.Title.ToString();
24 sbMenu.Append("<li><a ");
25 if (strUrl=="/Search")
26 {
27 sbMenu.Append(" target=\"_blank\" ");
28 }
29 sbMenu.Append("href=\"" + strUrl + "\">" + strTitle + "</a>");
30 if (node.ChildNodes.Count > 0)
31 {
32 sbMenu.Append("<ul>");
33 foreach (SiteMapNode nodechild in node.ChildNodes)
34 {
35 string strChildUrl = nodechild.Url.ToString();
36 string strChildTitle = nodechild.Title.ToString();
37 sbMenu.Append("<li><a ");
38 if (strChildUrl.Contains("http"))
39 {
40 sbMenu.Append(" target=\"_blank\" ");
41 }
42 sbMenu.Append("href=\"" + strChildUrl + "\">" + strChildTitle + "</a></li>");
43 }
44 sbMenu.Append("</ul>");
45 }
46 sbMenu.Append("</li>");
47 }
48 return sbMenu.ToString();
49 }
50 }
2 /// 获取站点导航
3 /// </summary>
4 /// <returns></returns>
5 private SiteMapNode GetSiteMapRootNodeOfCurrentWeb()
6 {
7 SiteMapProvider smp = PortalSiteMapProvider.CombinedNavSiteMapProvider;
8 SiteMapNode rootMap = smp.RootNode;
9 return rootMap;
10 }
11 /// <summary>
12 /// get menu html
13 /// </summary>
14 /// <returns></returns>
15 public string GetMenuString()
16 {
17 StringBuilder sbMenu = new StringBuilder();
18 SiteMapNode smn = GetSiteMapRootNodeOfCurrentWeb();
19 sbMenu.Append("<li><a href=\"/\">首页</a>");
20 foreach (SiteMapNode node in smn.ChildNodes)
21 {
22 string strUrl = node.Url.ToString();
23 string strTitle = node.Title.ToString();
24 sbMenu.Append("<li><a ");
25 if (strUrl=="/Search")
26 {
27 sbMenu.Append(" target=\"_blank\" ");
28 }
29 sbMenu.Append("href=\"" + strUrl + "\">" + strTitle + "</a>");
30 if (node.ChildNodes.Count > 0)
31 {
32 sbMenu.Append("<ul>");
33 foreach (SiteMapNode nodechild in node.ChildNodes)
34 {
35 string strChildUrl = nodechild.Url.ToString();
36 string strChildTitle = nodechild.Title.ToString();
37 sbMenu.Append("<li><a ");
38 if (strChildUrl.Contains("http"))
39 {
40 sbMenu.Append(" target=\"_blank\" ");
41 }
42 sbMenu.Append("href=\"" + strChildUrl + "\">" + strChildTitle + "</a></li>");
43 }
44 sbMenu.Append("</ul>");
45 }
46 sbMenu.Append("</li>");
47 }
48 return sbMenu.ToString();
49 }
50 }
此处获取的是左侧的快速导航栏,同样,也需要在网站设置-导航里面去设置
需要隐藏的对应代码,设置style="display:none;"
<div id="s4-mainarea" class="s4-pr s4-widecontentarea">
<div id="s4-leftpanel" class="s4-notdlg">
<div id="s4-leftpanel-content" style="display:none;">
需要添加的用户控件
<%@ Register TagPrefix="wssuc" TagName="ListLeft" src="~/_controltemplates/ECISystem/ListLeft.ascx" %>
<wssuc:ListLeft runat="server" id="ListLeft"></wssuc:ListLeft>
<wssuc:ListLeft runat="server" id="ListLeft"></wssuc:ListLeft>
具体代码
///get menu html
1 public string GetMenuString(SPWeb web)
2 {
3 StringBuilder sbMenu = new StringBuilder();
2 {
3 StringBuilder sbMenu = new StringBuilder();
26 //SPNavigationNodeCollection topLinkBar = web.Navigation.TopNavigationBar;//这个是顶部导航的快速链接。也可以通过这个获取
27 SPNavigationNodeCollection quickLinkBar = web.Navigation.QuickLaunch;
28 int i = 2;
29 foreach (SPNavigationNode spn in quickLinkBar)
30 {
31 i++;
32 sbMenu.Append("<li><span style=\"vertical-align:middle;\"><img src=\"/_layouts/images/EciOA/n" + i.ToString() + ".jpg\" width=\"17\" height=\"18\" /></span> ");
33 sbMenu.Append("<a href='" + spn.Url + "'>" + spn.Title + "</a><span style=\"font-size:13px; font-weight:bold; color:#C30;\"></span></li>");
34 }
35 return sbMenu.ToString();
36 }
27 SPNavigationNodeCollection quickLinkBar = web.Navigation.QuickLaunch;
28 int i = 2;
29 foreach (SPNavigationNode spn in quickLinkBar)
30 {
31 i++;
32 sbMenu.Append("<li><span style=\"vertical-align:middle;\"><img src=\"/_layouts/images/EciOA/n" + i.ToString() + ".jpg\" width=\"17\" height=\"18\" /></span> ");
33 sbMenu.Append("<a href='" + spn.Url + "'>" + spn.Title + "</a><span style=\"font-size:13px; font-weight:bold; color:#C30;\"></span></li>");
34 }
35 return sbMenu.ToString();
36 }