Welcome to MSDN Blogs Sign in | Join | Help

Chris Johnson

All about Chris Johnson, SharePoint Products & Technologies & Other Stuff.

News

My Sub Sites Web Part...

One web part that i think is missing in the out of the box parts is one that allows you to show the sub-sites of a site.  There are a couple out there already ... but i had the need to only show sub-sites that the user had access to. 

Anyway ... here is the code:

 [DefaultProperty("Text"),
  ToolboxData("<{0}:MySubSitesWebPart runat=server></{0}:MySubSitesWebPart>"),
  XmlRoot(Namespace="MySubSites")]
 public class MySubSitesWebPart : Microsoft.SharePoint.WebPartPages.WebPart
 {
  private const string defaultText = "";
  private string text=defaultText;

  private string title = "My Sub Sites";

  [Browsable(true),Category("Miscellaneous"),
   DefaultValue(defaultText),
   WebPartStorage(Storage.Personal),
   FriendlyName("Text"),Description("Text Property")]
  public string Text
  {
   get
   {

    StringBuilder builder = new StringBuilder();

    text = "";

    SPWeb currentSite = SPControl.GetContextWeb(Context);

    SPWebCollection subSites = currentSite.GetSubwebsForCurrentUser();
    
    builder.Append(@"<table border='0' width='100%'>");
 
    foreach(SPWeb site in subSites)
    {
     builder.Append("<tr><td class='ms-underlineback'>");
     builder.Append("<a href='");
     builder.Append(site.Url);
     builder.Append("' style='cursor:hand' title=''>");
     builder.Append(SPEncode.HtmlEncode(site.Title));
     builder.Append(@"</a></td></tr>");
    }

    builder.Append(@"</table>");

    return builder.ToString();
   }

   set
   {
    text = value;
   }
  }
  
  
  /// <summary> 
  /// Render this Web Part to the output parameter specified.
  /// </summary>
  /// <param name="output"> The HTML writer to write out to </param>
  protected override void RenderWebPart(HtmlTextWriter output)
  {
   output.Write(Text);
  }
 }

Posted: Thursday, May 12, 2005 11:49 PM by chjohn
Filed under: ,

Comments

Bruce Barton said:

CJ, can you tell me how to add this to my sites next time you are visiting? I will try and remind you.

Cheers,
# May 15, 2005 5:09 AM

miglena said:

Very nice blog. It is very helpful. http://www.bignews.com
# August 19, 2005 3:11 AM

David Sterling said:

Nice tip - glad to see the share; interesting to use the Property to create the actual output string.
# August 15, 2006 7:17 PM
Leave a Comment

(required) 

(required) 

(optional)

(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