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("
" + SPEncode.HtmlEncode(siteCollection.RootWeb.Title) + " (RootWeb) | "); writer.Write("" + siteCollection.RootWeb.Url + " | "); writer.WriteLine("" + SPEncode.HtmlEncode(ConvertBytesToMegabytes(GetWebSize(siteCollection.RootWeb)).ToString("0.00")) + " MB | "); writer.WriteLine("
" + SPEncode.HtmlEncode(subSite.Title) + " | "); writer.Write("" + subSite.Url + " | "); writer.WriteLine("" + SPEncode.HtmlEncode(ConvertBytesToMegabytes(GetWebSize(subSite)).ToString("0.00")) + " MB | "); writer.WriteLine("
----- | ----- | ----- |
Total size of sitecollection | "); writer.Write("" + siteCollection.Url + " | "); writer.WriteLine("" + SPEncode.HtmlEncode(ConvertBytesToMegabytes(siteCollection.Usage.Storage).ToString("0.00")) + " MB | "); writer.WriteLine("
No comments:
Post a Comment