SharePoint Development Blog

Nick Boumans
View my LinkedIn Profile Follow me on Twitter View my Profile on FaceBook View my projects on CodePlex View my presentations on SlideShare



Recent posts

Tags

Categories

Navigation

Pages

Archive

Blogroll

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

Site Storage WebPart

For SharePoint site Administrators it could be important to view the storage of a SiteCollection (and subsites). However, SharePoint sites are often hosted by a external hosting and administrators have no access to the Central Administration. I wrote the following WebPart which summarizes the storage of a SiteCollection and the SubSites of this SiteCollection. Since there's no property for getting the Site Size I make recursively use of the Folder Size of a SPWeb.

public class SiteUsageWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
    public SiteUsageWebPart()
    {

    }

    private SPSite siteCollection;
    private SPWeb site;
    private SPWebCollection sites;       

    protected override void CreateChildControls()
    {
        siteCollection = SPContext.Current.Site;
        site = SPContext.Current.Web;
        sites = siteCollection.RootWeb.Webs;                       
    }

    private long GetWebSize(SPWeb web)
    {
        long total = 0;

        foreach (SPFolder folder in web.Folders)
        {
            total += GetFolderSize(folder);
        }

        foreach (SPWeb subweb in web.Webs)
        {
            total += GetWebSize(subweb);
            subweb.Dispose();
        }

        return total;
    }

    private long GetFolderSize(SPFolder folder)
    {
        long folderSize = 0;

        foreach (SPFile file in folder.Files)
        {
            folderSize += file.Length;
        }

        foreach (SPFolder subfolder in folder.SubFolders)
        {
            folderSize += GetFolderSize(subfolder);
        }

        return folderSize;
    }

    private double ConvertBytesToMegabytes(long bytes)
    {
        return (bytes / 1024f) / 1024f;
    }


    protected override void Render(HtmlTextWriter writer)
    {
       
        writer.WriteLine("<table>");
       
        // Size of the Rootweb
        writer.WriteLine("<tr>");
        writer.Write("<td><B>" + SPEncode.HtmlEncode(siteCollection.RootWeb.Title) + " (RootWeb)</B></td>");
        writer.Write("<td>" + siteCollection.RootWeb.Url + "</td>");
        writer.WriteLine("<td>" + SPEncode.HtmlEncode(ConvertBytesToMegabytes(GetWebSize(siteCollection.RootWeb)).ToString("0.00")) + " MB</td>");
        writer.WriteLine("</tr>");

        foreach (SPWeb subSite in sites)
        {
            writer.WriteLine("<tr>");
            writer.Write("<td><B>" + SPEncode.HtmlEncode(subSite.Title) + "</B></td>");
            writer.Write("<td>" + subSite.Url + "</td>");
            writer.WriteLine("<td>" + SPEncode.HtmlEncode(ConvertBytesToMegabytes(GetWebSize(subSite)).ToString("0.00")) + " MB</td>");
            writer.WriteLine("</tr>");
            //SPListCollection lists = subSite.Lists;

            //foreach (SPList list in lists)
            //{
            //    writer.Write(SPEncode.HtmlEncode(list.Title)
            //        e.g. you can also summarize list sizes
            //}
        }
        // Total Size of SiteCollection
        writer.WriteLine("<tr><td>-----</td><td>-----</td><td>-----</td></tr>");
        writer.WriteLine("<tr>");
        writer.Write("<td><B>Total size of sitecollection </B></td>");
        writer.Write("<td>" + siteCollection.Url + "</td>");
        writer.WriteLine("<td><B>" + SPEncode.HtmlEncode(ConvertBytesToMegabytes(siteCollection.Usage.Storage).ToString("0.00")) + " MB</B></td>");
        writer.WriteLine("</tr>");

        writer.WriteLine("</table>");
    }
}

Posted: Nov 17 2009, 23:07 by Nick Boumans | Comments (0) RSS comment feed |
  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Web Parts

Comments