Custom Site Map Web Part using dynamic Asp Menu


Site Map is a very important feature for the users to have a high level view of all sites and even for cross site navigation. I have implement recently a very basic Site map webpart which iterate through all the site collections with in a web application and display them as a tree view like structure.

I am sharing the code which basically contains a visual web part:

For Ascx file:

<asp:Menu ID=”SiteMapMenu” runat=”server” StaticDisplayLevels=”3″ Font-Size=”16px” Font-Bold=”true” DynamicMenuItemStyle-ItemSpacing=”5px” DynamicHorizontalOffset=”10″ >
<StaticSelectedStyle ForeColor=”#A5C8E8″ BackColor=”#A5C8E” />
<StaticMenuItemStyle HorizontalPadding=”20px” VerticalPadding=”3px” />
<DynamicHoverStyle BackColor=”#A5C8E8″ />
<DynamicMenuStyle BackColor=”#A5C8E8″ BorderStyle=”Solid” BorderWidth=”1px” BorderColor=”#4F86BC” />
<DynamicSelectedStyle BackColor=”#A5C8E8″ />
<DynamicMenuItemStyle HorizontalPadding=”5px” VerticalPadding=”2px” />
<StaticHoverStyle BackColor=”#A5C8E8″ />

For code behind of the in CreateChildControls

protected override void CreateChildControls()
var currentUser = SPContext.Current.Web.CurrentUser.LoginName;
foreach (SPSite sc in SPContext.Current.Site.WebApplication.Sites)
var rootWeb = sc.RootWeb;
if (string.Equals(rootWeb.WebTemplate.ToUpper(), "SRCHCEN"))

var siteMapMenu = new Menu();
ConfigureMenu(ref siteMapMenu);

var siteCollectionNode = new MenuItem(rootWeb.Title, rootWeb.Title, "", rootWeb.Url);
if (rootWeb.DoesUserHavePermissions(currentUser, SPBasePermissions.Open))
var webCollection = rootWeb.GetSubwebsForCurrentUser();

foreach (SPWeb web in webCollection)
IterateSubWebs(web, currentUser, ref siteCollectionNode);
if (web != null)

var menuDiv = new HtmlGenericControl("div") {ID = rootWeb.Title + "Div"};
menuDiv.Attributes["style"] = "float: left; padding-right:10px";

 private static void ConfigureMenu(ref Menu siteMapMenu)
// move the styles to css
siteMapMenu.StaticDisplayLevels = 3;
siteMapMenu.Font.Size = FontUnit.Point(11);
siteMapMenu.Font.Bold = true;
siteMapMenu.DynamicMenuItemStyle.ItemSpacing = Unit.Pixel(5);
siteMapMenu.DynamicHorizontalOffset = 10;
siteMapMenu.StaticSelectedStyle.ForeColor = ColorTranslator.FromHtml("#E0E0E0");
siteMapMenu.StaticSelectedStyle.BackColor = ColorTranslator.FromHtml("#E0E0E0");
siteMapMenu.StaticSelectedStyle.HorizontalPadding = Unit.Pixel(20);
siteMapMenu.StaticSelectedStyle.VerticalPadding = Unit.Pixel(3);
siteMapMenu.DynamicHoverStyle.BackColor = ColorTranslator.FromHtml("#E0E0E0");
siteMapMenu.DynamicMenuStyle.BackColor = ColorTranslator.FromHtml("#E0E0E0");
siteMapMenu.DynamicMenuStyle.BorderStyle = BorderStyle.Solid;
siteMapMenu.DynamicMenuStyle.ForeColor = ColorTranslator.FromHtml("#E0E0E0");
siteMapMenu.DynamicMenuStyle.BorderWidth = Unit.Pixel(1);
siteMapMenu.DynamicMenuStyle.BorderColor = ColorTranslator.FromHtml("#0C1F50");
siteMapMenu.DynamicSelectedStyle.BorderColor = ColorTranslator.FromHtml("#E0E0E0");
siteMapMenu.DynamicMenuStyle.HorizontalPadding = Unit.Pixel(10);
siteMapMenu.DynamicSelectedStyle.HorizontalPadding = Unit.Pixel(5);
siteMapMenu.DynamicSelectedStyle.VerticalPadding = Unit.Pixel(2);
siteMapMenu.StaticHoverStyle.BackColor = ColorTranslator.FromHtml("#E0E0E0");
siteMapMenu.StaticHoverStyle.BorderStyle = BorderStyle.Solid;
siteMapMenu.StaticHoverStyle.BorderWidth = Unit.Pixel(1);
siteMapMenu.StaticHoverStyle.BorderColor = ColorTranslator.FromHtml("#0C1F50");


private static void IterateSubWebs(SPWeb web, string user, ref MenuItem node)
var chieldNode = new MenuItem(web.Title, web.Title, "", web.Url);
if (web.DoesUserHavePermissions(user, SPBasePermissions.Open))
foreach (SPWeb subWeb in web.GetSubwebsForCurrentUser())
IterateSubWebs(subWeb, user, ref chieldNode);

Using the above code simply we can iterate through all the site collections and then sub sites of each site.

Happy SharePointing…:)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s