How to find out the storage space allocation details a site through code.

SharePoint has a cool and great facility called Quota templates. In order to get this facility in UI, you have to do the following. Refer: https://technet.microsoft.com/en-us/library/cc263223.aspx

Enable the site collection quotas in the central administration site – under application management.

clip_image002

Once you click on the “site collection quotas and locks” you will be redirecting to another page and there you can set your quota template and lock status.

clip_image004

Once you enable this one, then you can see a new link under the site collection administration section of your site.

clip_image006

Once you click on that link it will redirect to another beautiful page which is given below.

clip_image008

In the above page we can see all document libraries, documents and lists by selecting the “show only” drop down. Also here we can filter the result using the “Show items” drop down and also we can sort the list items using “Sort By” drop down.

The list will show the name of the list or library and the corresponding size and related information. Now we can see how we can retrieve this information through code. The below code is self explanatory and I believe it won’t confuse you any more J

We can go with the code if you want to create a custom view representation of this detail. Also it will help us if you want to get this information anywhere in your custom application.

    1: SPSite oSite = new SPSite("https://blr3r7-19c:13774/sites/testwp");
    2: DataTable oDtRawData = null;
    3:             
    4: // this line of code will return the stroage information of all the document lirbaries in this site
    5: oDtRawData = oSite.StorageManagementInformation(SPSite.StorageManagementInformationType.DocumentLibrary,SPSite.StorageManagementSortOrder.Increasing, SPSite.StorageManagementSortedOn.Size,100);
    6:  
    7: // this line of code will return the stroage information of all the lists in this site
    8: oDtRawData = oSite.StorageManagementInformation(SPSite.StorageManagementInformationType.List, SPSite.StorageManagementSortOrder.Increasing, SPSite.StorageManagementSortedOn.Size, 100);
    9:  
   10: // this line of code will return the stroage information of all the Documents in this site
   11: oDtRawData = oSite.StorageManagementInformation(SPSite.StorageManagementInformationType.Document, SPSite.StorageManagementSortOrder.Increasing, SPSite.StorageManagementSortedOn.Size, 100);
   12:             
   13: // if you wan to see column names, loop through all the columns and find out the names and grab the needed one. 
   14: foreach (DataColumn oColumn in oDtRawData.Columns)
   15:    {
   16:             Console.WriteLine(oColumn.ColumnName);                
   17:    }
   18: Console.ReadLine();
   19:  
   20: // loop through all the rows and find out the values. Here the size will be return in bytes (size/1024 = size in KBs)
   21:   foreach (DataRow oRow in oDtRawData.Rows)
   22:    {
   23:          Console.WriteLine(oRow["Title"].ToString() + " : " + oRow["Size"].ToString() + " : " + oRow["LeafName"].ToString());               
   24:    }
   25:  
   26: Console.ReadLine();