Display Site Collection Quota on the SharePoint 2013 home's page

I came across a business requirement where end users can view the site collection quota (or site storage) on the home page.
 
Business requirement
 
If the quota site exceeds 70%, then show the quota in amber colour and it exceeds 80%, then show the quota in red colour. It means end users have to clean up the site and purge unnecessary Obviously this cannot be out of the box. While it can be achieved different ways such as:
  1. Use Server Object model.
  2. Use Client Object model.
  3. Deploy custom master page and apply as deployment control.

We can use server side code to achieve this requirement.
 
Technical Design
 
A couple of things have to consider and use appropriate approach for this solution. We will need:
  1. Visual Web Part – place drag drop objects.
  2. Custom Web Properties – fetch the custom web part used in the web parts for the web site URL.
  3. Access the storage objects (site.Usage.Storage and site.Quota.StorageMaximumLevel) via server object model.
  4. Storage, audit objects etc. cannot be used contributors or visitors have not enough permissions. In this case we require SPSecurity.RunWithElevatedPrivileges

Step by step
 
1. Create a new Visual Studio 2015 project >> SharePoint 2013 - Visual Web Part
 
 
2. Specify the web application you want to deploy this web part to and choose deploy this web past as farm solution for debugging.
 
 
 
3. Coding for VisualWebPart1.cs. 

 
The site collections administrator needs to place website URL for the site that we need to show the site quota for. We need a string variable to hold this variable and name it siteURL.


 
We also need to web part properties as well.




 
 
 
Visual Studio automatically create User Control. In CreateChildControl() method create a VisualWebPart1UserControl object SiteQuotaWebPart is defined on VisualWebPart1UserControl in next step.


 
 
 
4. Coding for VisualWebPart1UserControl.ascx.cs


 
Create a public constructor for SiteQuotaWebPart. Please note, we can't use static variable to pass siteURL values between passing values from VisualWebPart1.cs. to VisualWebPart1UserControl.ascx.cs as the static variable always keep the retain the same value.




 
 
 
In the page load for user control, impersonate and wrap the code with SPSecurity.RunWithElevatedPrivileges. 




 
 
 
Check if the site exists 



 
 
 
Access the Site Quota Objects via Server Object Model. 


 
First get an create a long variable named storageUsedMax and this object stores Site Quote template size that is defined by site collection administrator. 


 
Secondly get an create a long variable named storageUsed and actual size used by the site collection.





 
 
 
Next, compute remaining and total size as the percentage.

 
After this computation, we can assign the colour code depending on the size of the site collection.


 
 
 
5. Used a static function to return the size of the site collection in GBs, MBs. KBs or Bytes. 


 
6. Activate and deploy the site collection. But I still get an error as shown:


 
Error: A Web Part or Web Form Control on this page cannot be displayed or imported. You don’t have Add and Customize Pages permissions required to perform this action


 
 
 
7. Fix:

 
The solution to the problem is to navigate to the SharePointProjectItem.spdata file, associated with the web part and change the IsSafeAgainstScript to true in the <SafeControl.. IsSafeAgainstScript="true" /> element as shown:


 
 
 
8. Re-activate and deploy the solution.
 
9. Edit the home page, click on Insert tab, Under Categories locate Custom, Select "Display Size Quota", Click on "Add"


 
 
 
10. Edit Web Part


 
 
 
11. In the custom Web Part Properties, specific the site collection URL
.
 
 
 
12. Save the page.


 
 
 
13. Show the web part in different site collection sizes as shown:




 
 
 
 
Download the source code from here.
Ebook Download
View all
Learn
View all