Tags

,

This webpart will list all the sites you have permission to access.

Create a webpart project in vs 2005/2008,below is the source code:

[Guid("56d8a90d-8186-4333-b05d-f3b6b47a132c")]
    public class SiteList : System.Web.UI.WebControls.WebParts.WebPart
    {
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            try
            {
                SPWeb Web = SPControl.GetContextWeb(Context);
                SPWebCollection webs = Web.GetSubwebsForCurrentUser();
              
                if (webs.Count == 0)
                {
                }
                else
                {
                    SPTreeView siteTreeView = new SPTreeView();
                    siteTreeView.ExpandDepth = 10;
                    int i = 0;
                    foreach (SPWeb webToDisplay in webs)
                    {                    
                        siteTreeView.Nodes.Add(new TreeNode(webToDisplay.Title, webToDisplay.Url, "", webToDisplay.Url, ""));

                        TreeNode topNode = siteTreeView.Nodes[i];
                        ProcessWeb(topNode, webToDisplay);
                        i++;
                    }
                    Controls.Add(siteTreeView);
                }
            }
            catch (Exception ex)
            {
                Controls.Clear();
                Label errorMessage = new Label();
                errorMessage.Text = "There was an error in the code.  Please contact your system administrator and rely the following " +
                    "message: " + ex.Message;
                Controls.Add(errorMessage);
            }
        }

        private void ProcessWeb(TreeNode topNode, SPWeb subsite)
        {
            SPWebCollection webs = subsite.GetSubwebsForCurrentUser();
            foreach (SPWeb webToDisplay in webs)
            {
                topNode.ChildNodes.Add(new TreeNode(webToDisplay.Title, webToDisplay.Url, "", webToDisplay.Url, ""));

                 ProcessWeb(topNode, webToDisplay);

            }
        }
    }

Advertisements