In SharePoint all user content i.e. documents, list items etc is stored in the Content Database associated with a Site Collection. One question that administrators always ask is if they can restrict the database\disk usage for a given Site Collection. This is really usefull for My Sites and the good news is that this functionality is provided OOTB using Quotas.

The concept is that you create a Quota template by using UI elements in Central Administration, the template collection is available as a drop down list when ever you are provisioning a new Site Collection on the New Site Collection page. Adding and removing quotas can also be done any time after the site is created.

So far so good, today my colleague Joshua (Joshua Tan) relayed the requirements of a current project where the customer wanted to keep the users informed of thier quota use and I put on my code churn cap. To restate, the problem is how to communicate quota usage to users so they know if they have space remaining on thier MySites or any other Site for that matter.

Showing the user the amount of quota left/used should not be too hard? I asked my self and it turned out to be easier then I thought! In less then 30 minutes I had a basic working prototype. Given below are glory shots of the Site Quota web part in action. You can always add additional details like the amount of disk space available and maybe other improvements which I have listed at the end.

sitequotawebpartsitescreenshot.png
Site Quota web part on the site’s Home page with 250 MB Maximum Disk Space

sitequotawebpart.png
Site Quota web part by itself – gradient starts from green and grows to a fire engine red

Here is the code I wrote with the usefull parts highlighed.  It is provided as-is, use at your own risk: 

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace SiteQuotaWebPart
{
    [Guid(“eed45f6b-40ab-45d7-b148-41697a307a79”)]
    public class SiteQuotaWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        private int quotaUse = 0;
        private void CalculateQuotaUse()
        {
            SPSite siteCollection = SPContext.Current.Site;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite elevatedSiteCollection = new SPSite(siteCollection.ID))
                {
                    this.quotaUse = Convert.ToInt32(elevatedSiteCollection.Usage.Storage * 100 / elevatedSiteCollection.Quota.StorageMaximumLevel);
                }
            });

        }
        protected override void Render(HtmlTextWriter writer)
        {
            this.CalculateQuotaUse();
            writer.Write(“<style type=’text/css’>.QG {background-image: url(‘http://moss/QuotaGradient.png&#8217;);}</style>”);
            writer.Write(“<p><br><center><table style=’width:” + this.quotaUse * 2 + “px’ class=’QG’ cellspacing=’0′ cellpadding=’0′><tr><td> </td></tr></table></p>”);
            writer.Write(“<p><span class=’ms-pageinformationheader’>Quota Used: ” + this.quotaUse + “%</span></p></center>”);
        }
    }
}

A number of improvements come to mind and will be added in the final production release:

  • Show disk space used in megabytes
  •  Show total disk space in megabytes
  • Allow web part configuration to change the formatting of HTML
  • Allow arbitury values for Sites or Site Collection where Quotas can’t be enforced or don’t exist
  • Email notification to the owner of a site (not a site collection) when site uses more then alloted quota (the email should be generated only once regardless of how many instances of the Quota Web Part Exist)
  • Package the gradient image resource in the Web Part Assembly for ease of deployment
  • The webpart can be packed as a feature.
  • Error handling…
Advertisements