Here is the code:
ASCX File
<asp:TreeView ID="treeViewSiteNav" runat="server"> </asp:TreeView>
ASCX.CS File
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;
protected void Page_Load(object sender, EventArgs e)
{
treeViewSiteNav.Nodes.Clear();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
// Display the entire navigation structure of the web application
SPSiteCollection sites = SPContext.Current.Site.WebApplication.Sites;
foreach (SPSite currentSite in sites)
{
MapCurrentSiteCollection(currentSite.ID, currentSite.RootWeb.ID);
currentSite.Dispose();
}
});
}
private void MapCurrentSiteCollection(Guid siteId, Guid webId)
{
// Creating a new instance of the site because the currentSite is associated with the context of the
// user whereas this new instance will run with elevated privileges
using (SPSite site = new SPSite(siteId))
{
site.CatchAccessDeniedException = false;
using (SPWeb currentWeb = site.OpenWeb(webId))
{
TreeNode siteNode = new TreeNode(currentWeb.Title, null, null, currentWeb.Url, "_self");
SetCurrentNodeFont(ref siteNode, SPContext.Current.Web.ID, currentWeb.ID);
treeViewSiteNav.Nodes.Add(siteNode);
foreach (SPWeb web in currentWeb.Webs)
{
AddWebNodes(web, siteNode);
web.Dispose();
}
}
}
}
private void SetCurrentNodeFont(ref TreeNode node, Guid currentWebId, Guid evaluatedWebId)
{
if (currentWebId == evaluatedWebId)
{
node.Text = Server.HtmlDecode(string.Format("<b>{0}</b>", node.Text));
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.