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("<>"); // Size of the Rootweb writer.WriteLine(" >
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 |
No comments:
Post a Comment