6

Is there any REST api / any approach to get the size of list and library in a site collection using client object model.? I know we have SPSite.StorageManagementInformation but this is can be used in SSOM only, do we have anything similar to that in CSOM. For document library, I don't want to loop through all the documents in that library and get item.File.length as shown below, as I have more than 2000 site collection, if I use the below loop, then it would take a decade to get the size of SharePoint library.

if (item.File != null)
{
 listSize += item.File.Length;
}

but the above code can not be used for SharePoint list, in this case how to get the size of a SharePoint list.

1

You can get total TotalFileStreamSize and TotalSize of a document library from StorageMetrics. Make a GET request to following end-point.

/_api/web/getFolderByServerRelativeUrl('relative path of library')?$select=StorageMetrics&$expand=StorageMetrics

PS: Tested in SharePoint online and it works for libraries

Sample Response

{
    "d": {
        "__metadata": {
            "id": "https://xxx.sharepoint.com/sites/sm/_api/Web/GetFolderByServerRelativeUrl('/sites/sm/SiteAssets')",
            "uri": "https://xxx.sharepoint.com/sites/sm/_api/Web/GetFolderByServerRelativeUrl('/sites/sm/SiteAssets')",
            "type": "SP.Folder"
        },
        "StorageMetrics": {
            "__metadata": {
                "id": "https://xxx.sharepoint.com/sites/sm/_api/Web/GetFolderByServerRelativeUrl('/sites/sm/SiteAssets')/StorageMetrics",
                "uri": "https://xxx.sharepoint.com/sites/sm/_api/Web/GetFolderByServerRelativeUrl('/sites/sm/SiteAssets')/StorageMetrics",
                "type": "SP.StorageMetrics"
            },
            "LastModified": "2016-11-16T09:15:07Z",
            "TotalFileCount": "6",
            "TotalFileStreamSize": "292912",
            "TotalSize": "348099"
        }
    }
}

TotalFileStreamSize is the actual size of your library in bytes. You can using my REST Client for testing/exploring.

  • the API which you mentioned doesn't return anything. I'm not using SharePoint online, it's enterprise which I'm trying to connect from a console app using csom – Karthik Jaganathan Nov 21 '16 at 12:41
  • It only returns the last modified of a list, not the other information's. – Karthik Jaganathan Nov 21 '16 at 12:47
  • okay. Me tested on SharePoint Online. Right now I can not test in On-Premise. :( – Atish Dipongkor - MVP Nov 21 '16 at 12:50
0

I don't think so, because Documents are not stored a filesystem but in (rows in) a Database. These rows are chunks of data, not bytes.

So the size of a Document is metadata (written on file upload/change), not space occupied in the database.

That means total size of a Library can only be found with a loop

Alternatively you could add your own administration on every Document change. Maybe a workflow that keeps track of file-sizes.

0

I don't know if this will help you but with PowerShell you can get data from storage metric.

$Site = Get-SPsite "http://your.sharepoint.site"     
$DataTable = $Site.StorageManagementInformation(2, 0x11, 0, 0)     
foreach($Row in $DataTable.Rows)
{
    if ($Row.Title -eq "Shared Documents" -and $Row.Directory -eq "team")
    {
        $LibrarySize = [Math]::Round(($Row.Size/1MB), 2)
        Write-Host $LibrarySize "MB"
    }
}

This could be an approach to your problem.

0

If this is just for reporting purpose we can go with out of the box Storage Metrics report:

Navigation: Site Actions >> Site Settings >> Storage Metrics

enter image description here

Storage metrics report

Note:

We can get SharePoint library size, ONLY when you have Quotas applied to the site collection. otherwise, you get "The storage space allocation page cannot be used for sites that do not have a storage quota defined." error message in MOSS 2007.

Error in storage metrics page

How to find a particular document library's size in SharePoint programmatically?

Approach 1: Using StorageManagementInformation API: It retrieves storage management information about the site collection. More info on StorageManagementInformation , It takes these parameters:

  1. ltVar: Which object List = 1 DocumentLibrary = 2 Document = 3
  2. sordVar: sort order Increasing = 0×10 Decreasing = 0×11
  3. soVar: Sort based on size or date Size=0 Date = 1
  4. nMaxResults: number of results to return

Lets get the size of the "Shared Documents" Library with PowerShell:

#SharePoint library size report
#Get the Site collection
$Site = Get-SPsite "http://sharepoint.crescent.com"

#Returns a DataTable similar to "Storage Management Page" in Site settings
$DataTable = $Site.StorageManagementInformation(2,0x11,0,0)

#Loop through the Rows and Fetch the row matching "Shared Documents" in subsite "team"
 foreach($Row in $DataTable.Rows)
{
    if ($Row.Title -eq "Shared Documents" -and $Row.Directory -eq "team")
        {
            $LibrarySize = [Math]::Round(($Row.Size/1MB),2)
            Write-Host $LibrarySize "MB"
        }
}

We can retrieve Top 10 large libraries based on their size as:

#Get the Site collection
$Site = Get-SPsite "http://sharepoint.crescent.com"

#Returns a DataTable similar to "Storage Management Page" in Site settings
$DataTable = $Site.StorageManagementInformation(2,0x11,0,10)

$DataTable | Select Title, ItemCount, Size, Directory | Format-Table

Output:

Result - Output

Approach 2: Iterate through each document and its versions stored in the library to calculate the library size:

Lets use PowerShell to iterate each folder & sub-folder of a particular document library. This script can be used in MOSS 2007 or SharePoint 2010 get library size.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

# Function to calculate Library size
Function GetLibrarySize($Folder)
{
    $FolderSize = 0
    foreach ($File in $Folder.Files)
    {
        #Get File Size
        $FolderSize += $File.TotalLength;

        #Get the Versions Size
        foreach ($FileVersion in $File.Versions)
        {
            $FolderSize += $FileVersion.Size
        }
    }

      #Get Files in Sub Folders
        foreach ($SubFolder in $Folder.SubFolders)
        {
           if($SubFolder.Name -ne "Forms") #Leave "Forms" Folder which has List default Aspx Pages.
             {
                 $FolderSize += GetLibrarySize($SubFolder)
             }
        }

       return [Math]::Round(($FolderSize/1MB),2)
}

$Web = Get-SPWeb "http://sharepoint.crescent.com/team/"

#Get the Library's Root Folder
$Library =  $Web.Lists["Shared Documents"].RootFolder

#Call the function to Calculate Size
$LibrarySize=GetLibrarySize($Library)

Write-Host "Library Size:" $LibrarySize "MB"

Reference URL:

Get SharePoint Library Size with PowerShell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.