Welcome to MSDN Blogs Sign in | Join | Help

Pranab Paul's Blog - Development Tips on SharePoint, Office and Web

------------------------------------------Web Parts, Workflow, InfoPath Form Services, Features, Site Definition, Event Receivers, Excel Services, Business Data Catalog (BDC), Search

News

A SharePoint 2007 (MOSS/WSS) WebPart to show up all subsites under a site

Here is the code for the WebPart:

 

using System;

using System.Runtime.InteropServices;

using System.Web.UI;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Serialization;

 

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

 

namespace ListSubSites

{

    [Guid("e96ea212-f44b-4d16-886d-e8fe7d389a7a")]

    public class ListSubSites : System.Web.UI.WebControls.WebParts.WebPart

    {

 

        string output = "<table border=\"0\">";

     

        protected override void CreateChildControls()

        {

            base.CreateChildControls();

            SPSite site = new SPSite("http://<Your site URL>/");

            SPWeb web = site.OpenWeb();

            SPWebCollection subSites = web.Webs;

            foreach (SPWeb subSite in subSites)

            {

                output += "<tr><td><a href=\"" + subSite.Url + "\"/>" + subSite.Title + "</a></td></tr>";

            }

            output += "</table>";

        }

       

        protected override void Render(HtmlTextWriter writer)

        {

            // TODO: add custom rendering code here.

            EnsureChildControls();

            writer.Write(output);     

        }

       

    }

}

Posted: Wednesday, July 18, 2007 3:13 PM by pranab

Comments

Paulos said:

Hi there,

I was just wondering why you import

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

then inherit

System.Web.UI.WebControls.WebParts.WebPart?

Cheers

# October 22, 2007 4:30 AM

MOSS 2007 User said:

Ok tell me how I can use this code in my MOSS 2007 site, may be as webpart os something to show only subsites and not other contenets?

Thanks

# July 24, 2009 10:13 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker