Custom Site Map Web Part using dynamic Asp Menu


Hi,

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″ />
</asp:Menu>

For code behind of the in CreateChildControls

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

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)
{
try
{
IterateSubWebs(web, currentUser, ref siteCollectionNode);
}
finally
{
if (web != null)
web.Dispose();
}

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

 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);
}
node.ChildItems.Add(chieldNode);
}
}

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

Happy SharePointing…:)

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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