Load storage metric from SharePoint Online site collection
I have a requirement to query the storage metric information from SharePoint Online site collection, so basicially I try to use the following code without any luck.
///Bad sample
using (var clientContext = InitializeContext(context))
{
var storageMetrics = clientContext.Web.RootFolder.StorageMetrics;
clientContext.Load(storageMetrics);
clientContext.ExecuteQuery();
Console.WriteLine(storageMetrics.TotalSize);
//Error, no information is returned!!!
}
Then I tried to remember the API from the On-Premise SharePoint, and we need to enable the SPWeb.IncludeStorageMetrics before retrieving the storage metrics information. However there is no property Web.IncludeStorageMetrics in CSOM API!!! So how to figure out this? Let’s try to research the SharePoint On-Premise Code to know who enable the property in the background for the CSOM.
With free decompile tool, I found the following code in SPFolder class, and it means you need to load the Folder and the StorageMetrics property together to retrieve this information.
internal void OnQuerying(ClientQuery query, ClientQuery childItemQuery)
{
if (query != null && query.ContainsProperty("StorageMetrics"))
{
this.m_Web.IncludeStorageMetrics = true;
}
}
So I changed the code like this, and I finally got the report!
using (var clientContext = InitializeContext(context))
{
var rootFolder = clientContext.Web.RootFolder;
clientContext.Load(rootFolder, f => f.StorageMetrics);
clientContext.ExecuteQuery();
Console.WriteLine(rootFolder.StorageMetrics.TotalSize);
}
And the REST API sample is
https://tenant.sharepoint.com/_api/web/rootfolder?$select=storagemetrics&$expand=storagemetrics
Comments