Background
I was recently asked to supply a breakdown of the Document Library sizes within a SharePoint Site Collection (SPSite). I had a look through the SharePoint API and couldn’t find any obvious information on the SPDocumentLibrary or SPList objects.
The first solution I came up with was to use some recursion to enumerate through each SPWeb in the Site Collection and then enumerate through each Document Library in the web, adding the file sizes in each library as I went. This approach worked but took a huge amount of time.
At previous clients, it would have been acceptable to dive into the Content Database for the site collection and write a query to do the work, unfortunately at the current site there are decent policies and rules in place to stop this from being done.
SPSite.StorageManagementInformation
By chance, I found a method on the SPSite object called StorageManagementInformation
1
2
3
4
5
6
7
8
| using (SPSite site = new SPSite(url))
{
DataTable storageInfo;
storageInfo = site.StorageManagementInformation(
SPSite.StorageManagementInformationType.DocumentLibrary,
SPSite.StorageManagementSortOrder.Decreasing,
SPSite.StorageManagementSortedOn.Size, 2000);
} |
From here the data table can be queried as normal to output the information about each Document Library;
1
2
3
4
5
6
7
8
9
| foreach (DataRow row in storageInfo.Rows)
{
double size = Convert.ToDouble((row["Size"].ToString()) / 1024) / 1024;
Console.WriteLine(string.Format("{0}/{1}\t{2}\t{3}",
site.Url,
row["WebUrl"].ToString(),
row["Title"].ToString(),
size));
} |
More Information
There are several options for StorageManagementInformationType enum. Its worth noting that the row columns will be slightly different for each of them.