Custom Menu Driven features for showing any library folder structure upto Nth level.
Given various properties for modifying it or customizing it.
My custom menu will show all the top navigation links which you have added in the Navigation list under "Look and Feel" section and it will show all the library folder level structure which are shown under "Libraries" navigation section in Navigation List.
Add the below code in .Cs file of the webpart.
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
namespace CustomFeatures.CustomNavigation
{
[ToolboxItemAttribute(false)]
public class CustomNavigation : WebPart
{
public CustomNavigation()
{
this.Title = " View Screen ";
}
public string _MenuList = "spNavigationMenu";
[Personalizable, WebBrowsable, WebDisplayName("Menu List")]
public string Set_MenuList
{
get
{
return _MenuList;
}
set
{
_MenuList = value;
}
}
public string _ServerURL = "";
char[] fwdSlash = { '/' };
[Personalizable, WebBrowsable, WebDisplayName("Server URL")]
public string Set_ServerURL
{
get
{
if (_ServerURL.Trim().EndsWith("/"))
return _ServerURL.Trim().TrimEnd(fwdSlash);
else
return _ServerURL;
}
set
{
_ServerURL = value;
}
}
public string _WebSite = "";
[Personalizable, WebBrowsable, WebDisplayName("Web Site")]
public string Set_WebSite
{
get
{
return _WebSite;
}
set
{
_WebSite = value;
}
}
public string _MenuContainer = "ms-topnavContainer";
[Personalizable, WebBrowsable, WebDisplayName("Menu Container CSS (ms-topnavContainer)")]
public string Set_MenuContainer
{
get
{
return _MenuContainer;
}
set
{
_MenuContainer = value;
}
}
public string _StaticMenuItemStyle = "ms-topnav";
[Personalizable, WebBrowsable, WebDisplayName("Static Menu Item Style CSS (ms-topnav)")]
public string Set_StaticMenuItemStyle
{
get
{
return _StaticMenuItemStyle;
}
set
{
_StaticMenuItemStyle = value;
}
}
public string _StaticSelectedStyle = "ms-topnavselected";
[Personalizable, WebBrowsable, WebDisplayName("Static Selected Style CSS (ms-topnavselected)")]
public string Set_StaticSelectedStyle
{
get
{
return _StaticSelectedStyle;
}
set
{
_StaticSelectedStyle = value;
}
}
public string _StaticHoverStyle = "ms-topNavHover";
[Personalizable, WebBrowsable, WebDisplayName("Static Hover Style CSS (ms-topNavHover)")]
public string Set_StaticHoverStyle
{
get
{
return _StaticHoverStyle;
}
set
{
_StaticHoverStyle = value;
}
}
public string _DynamicMenuStyle = "ms-topnavContainer";
[Personalizable, WebBrowsable, WebDisplayName("Dynamic Menu Style CSS (ms-topnavContainer)")]
public string Set_DynamicMenuStyle
{
get
{
return _DynamicMenuStyle;
}
set
{
_DynamicMenuStyle = value;
}
}
public string _DynamicMenuItemStyle = "ms-topNavFlyOuts";
[Personalizable, WebBrowsable, WebDisplayName("Dynamic Menu Item Style CSS (ms-topNavFlyOuts)")]
public string Set_DynamicMenuItemStyle
{
get
{
return _DynamicMenuItemStyle;
}
set
{
_DynamicMenuItemStyle = value;
}
}
public string _DynamicHoverStyle = "ms-topNavFlyOutsHover";
[Personalizable, WebBrowsable, WebDisplayName("Dynamic Hover Style CSS (ms-topNavFlyOutsHover)")]
public string Set_DynamicHoverStyle
{
get
{
return _DynamicHoverStyle;
}
set
{
_DynamicHoverStyle = value;
}
}
public string _DynamicSelectedStyle = "ms-topNavFlyOutsSelected";
[Personalizable, WebBrowsable, WebDisplayName("Dynamic Selected Style CSS (ms-topNavFlyOutsSelected)")]
public string Set_DynamicSelectedStyle
{
get
{
return _DynamicSelectedStyle;
}
set
{
_DynamicSelectedStyle = value;
}
}
public string _MenuOrientation = "Horizontal";
[Personalizable, WebBrowsable, WebDisplayName("Menu Orientation (Horizontal or Vertical")]
public string Set_MenuOrientation
{
get
{
return _MenuOrientation;
}
set
{
_MenuOrientation = value;
}
}
public string _MenuName = "spNavMenu";
[Personalizable, WebBrowsable, WebDisplayName("Menu Name")]
public string Set_MenuName
{
get
{
return _MenuName;
}
set
{
_MenuName = value;
}
}
public string _TopNavigationMenu = "yes";
[Personalizable, WebBrowsable, WebDisplayName("Top Navigation Menu? (yes or no)")]
public string Set_TopNavigationMenu
{
get
{
return _TopNavigationMenu;
}
set
{
_TopNavigationMenu = value;
}
}
public string _MenuPreRenderClientScript = "";
[Personalizable, WebBrowsable, WebDisplayName("Menu Client Script")]
public string Set_MenuPreRenderClientScript
{
get
{
return _MenuPreRenderClientScript;
}
set
{
_MenuPreRenderClientScript = value;
}
}
/// <summary>
/// Folder level depth value
/// Try to avoid using the RenderWebPart() method.
/// </summary>
public int _MenuLevel = 6;
[Personalizable, WebBrowsable, WebDisplayName("Menu Level")]
public int Set_MenuLevel
{
get
{
return _MenuLevel;
}
set
{
_MenuLevel = value;
}
}
public string SetServerURL(string inURL)
{
if (inURL.ToLower().StartsWith("http"))
return inURL;
else
if (inURL.StartsWith("/"))
return Set_ServerURL + inURL;
else if (inURL.Length > 0)
return Set_ServerURL + "/" + inURL;
else
return "";
}
/// <summary>
/// Create all your controls here for rendering.
/// Try to avoid using the RenderWebPart() method.
/// </summary>
protected override void CreateChildControls()
{
//this.ExportMode = WebPartExportMode.All;
this.ChromeType = PartChromeType.None;
// AspMenu _spMenu;
AspMenu _spParentMenu;
string MenuID = _MenuList + "spCustomMenu";
// _spMenu = new AspMenu();
_spParentMenu = new AspMenu();
SPWeb thisWeb = null;
try
{
//Set some defaults
if (Set_TopNavigationMenu.ToLower() != "yes" && Set_TopNavigationMenu.ToLower() != "no")
Set_TopNavigationMenu = "yes";
if (Set_TopNavigationMenu.ToLower() == "yes")
{
Set_MenuName = "TopNavigationMenu";
Set_MenuOrientation = "horizontal";
}
_spParentMenu.ID = Set_MenuName;
_spParentMenu.EnableViewState = false;
if (Set_MenuOrientation.ToLower() == "horizontal")
{
_spParentMenu.Orientation = System.Web.UI.WebControls.Orientation.Horizontal;
_spParentMenu.StaticPopOutImageUrl = "/_ControlTemplates/CustomFeatures/right.png";
}
else _spParentMenu.Orientation = System.Web.UI.WebControls.Orientation.Vertical;
_spParentMenu.DynamicPopOutImageUrl = "/_ControlTemplates/CustomFeatures/left.png";
_spParentMenu.ScrollDownImageUrl = "/_ControlTemplates/CustomFeatures/top.png";
_spParentMenu.ScrollUpImageUrl = "/_ControlTemplates/CustomFeatures/bottom.png";
_spParentMenu.StaticMenuItemStyle.ItemSpacing = 0;
_spParentMenu.MaximumDynamicDisplayLevels = Set_MenuLevel;
_spParentMenu.StaticDisplayLevels = 1;
_spParentMenu.StaticSubMenuIndent = 0;
_spParentMenu.DynamicHorizontalOffset = 0;
_spParentMenu.StaticPopOutImageTextFormatString = "";
_spParentMenu.SkipLinkText = "";
_spParentMenu.CssClass = Set_MenuContainer;
_spParentMenu.StaticMenuItemStyle.CssClass = Set_StaticMenuItemStyle;
_spParentMenu.StaticSelectedStyle.CssClass = Set_StaticSelectedStyle;
_spParentMenu.StaticHoverStyle.CssClass = Set_StaticHoverStyle;
_spParentMenu.DynamicMenuStyle.CssClass = Set_DynamicMenuStyle;
_spParentMenu.DynamicMenuItemStyle.CssClass = Set_DynamicMenuItemStyle;
_spParentMenu.DynamicHoverStyle.CssClass = Set_DynamicHoverStyle;
_spParentMenu.DynamicSelectedStyle.CssClass = Set_DynamicSelectedStyle;
System.Web.UI.WebControls.MenuItem _spHomeMenuItem;
System.Web.UI.WebControls.MenuItem _spParentMenuItem;
System.Web.UI.WebControls.MenuItem _spParentMenuItem2;
System.Web.UI.WebControls.MenuItem _spParentMenuItem3 = new System.Web.UI.WebControls.MenuItem();
SPSite thisSite;
if (Set_ServerURL == "" || Set_ServerURL == null)
{
thisSite = new SPSite(SPContext.Current.Site.Url);
}
else
thisSite = new SPSite(Set_ServerURL);
if (Set_WebSite == "" || Set_WebSite == null)
{
thisSite = new SPSite(SPContext.Current.Site.Url);
thisWeb = thisSite.OpenWeb();
}
else
thisSite = new SPSite(Set_WebSite);
thisWeb = thisSite.OpenWeb();
SPNavigationNodeCollection navcol = thisWeb.Navigation.TopNavigationBar;/*Retrieve Top Navigation Menus */
SPQuery _spQuery = new SPQuery();
System.Web.UI.WebControls.MenuItem _spMenuItem = new System.Web.UI.WebControls.MenuItem();
System.Web.UI.WebControls.MenuItem _spMenuItem2 = new System.Web.UI.WebControls.MenuItem();
_spHomeMenuItem = new System.Web.UI.WebControls.MenuItem(thisWeb.Navigation.Home.Title);/*Show Home Menu */
_spHomeMenuItem.NavigateUrl = thisWeb.Navigation.Home.Url; ;
_spParentMenu.Items.Add(_spHomeMenuItem);
foreach (SPNavigationNode topNav in navcol)
{
_spParentMenuItem = new System.Web.UI.WebControls.MenuItem(topNav.Title);
_spParentMenuItem.NavigateUrl = topNav.Url;
foreach (SPNavigationNode childNode in topNav.Children)
{
_spParentMenuItem2 = new System.Web.UI.WebControls.MenuItem(childNode.Title);
_spParentMenuItem2.Target = "_self";
_spParentMenuItem2.NavigateUrl = childNode.Url;
_spParentMenuItem.ChildItems.Add(_spParentMenuItem2);
if (topNav.Title == "Libraries")
{
SPList _spListMenu = thisWeb.Lists[childNode.Title];
SPDocumentLibrary doclib = (SPDocumentLibrary)thisWeb.Lists[childNode.Title];
SPFolder root = _spListMenu.RootFolder;
SPFolderCollection spfcol = root.SubFolders;
foreach (SPFolder folder in spfcol)
{
if (folder.Name != "Forms")
{
_spMenuItem = new System.Web.UI.WebControls.MenuItem(folder.Name);
_spMenuItem.Target = "_self";
_spMenuItem.NavigateUrl = folder.ServerRelativeUrl;
GetListItems(folder.Name.ToString(), _spMenuItem, thisWeb, _spListMenu, doclib, folder);
_spParentMenuItem2.ChildItems.Add(_spMenuItem);
}
}
}
}
_spParentMenu.Items.Add(_spParentMenuItem);
}
// _spParentMenu.Attributes.Add("onmouseover","javascript:alert('hi');");
Controls.Add(_spParentMenu);
}
catch (Exception ex)
{
Controls.Add(new LiteralControl("An error has occured with this web part. Please contact your system administrator and relay this error message: " + ex.Message + " sub:CreateChildControls "));
}
finally
{
if (thisWeb != null)
thisWeb.Dispose();
}
}
private void GetListItems(string str, System.Web.UI.WebControls.MenuItem _spMenu, SPWeb web, SPList _spListMenu, SPDocumentLibrary doclib, SPFolder folder)
{
try
{
SPFolderCollection spfcol = web.GetFolder(folder.ServerRelativeUrl).SubFolders;
System.Web.UI.WebControls.MenuItem _spMenuItem = new System.Web.UI.WebControls.MenuItem();
foreach (SPFolder spf in spfcol)
{
_spMenuItem = new System.Web.UI.WebControls.MenuItem(spf.Name);
_spMenuItem.Target = "_self";
_spMenuItem.NavigateUrl = spf.ServerRelativeUrl;
GetListItems(spf.Name, _spMenuItem, web, _spListMenu, doclib, spf);
_spMenu.ChildItems.Add(_spMenuItem);
}
}
catch
{
}
}
protected override void OnPreRender(EventArgs e)
{
if (Set_MenuPreRenderClientScript.Length > 0)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), Set_MenuPreRenderClientScript, Set_MenuPreRenderClientScript + "(document.getElementById('" + this.ClientID + "'));", true);
}
base.OnPreRender(e);
}
}
}
Menu Level section is added for adding the depth level for the menu as a property in the webpart properties.
To give the style , styles properties provision is given in it.
Screenshot is attached of the Menu navigation made by me.