Setting Max and Warning Quotas globally on existing WSS sites

Refer to blogs.msdn.com/dwinter/archive/2005/02/15/373076.aspx for setup if you are not familiar with creating a SharePoint OM application.

This is a simple example of changing Max and Warning Storage quotas on all sites. This can be necessary if you wish to change the quotas on existing sites and not just new ones. I am showing a few new concepts over previous blog entries which will likely continue to be used in later blog entires.

First I want to point out that I am using a perhaps non-standard technique to allow for quick design and easy interface. This app requires:

  • a combobox named vsCombo
  • a listbox named vsList
  • a listbox named errorBox (which I'm logging errors/etc to)
  • two textboxes for input newMax and newWarn
  • a button called setNewValues.

You can pretty up the interface however you want beyond this and you should be able to drop this code in place. You'll want to register the combobox dropdown event and also the button click to match these functions.

I am doing something unique here by putting the URL of the virtualservers in a listbox and then putting the actual virtualserver objects in a listbox that is hidden (vsList.Visible=false). I then ensure that selectedindexchanged on the combobox also moves the index number in the listbox so we can access the right item later.

You may be saying.. I could have also accessed SPSite.Quota.StorageMaximumLevel and warninglevel from a simple SPSite object as in the DeleteSite example. However, the methods are only read only there. Set is allowed in intellisense but not in reality. So by getting an SPSite object off of a SPVirtualServer object, we are going through a different codepath that is allowing us to have both get and set. This may change in the future, but at the time I wrote this code--thats the way it was. It also allows me to show some enumeration code and some tricks, so why not.

private void vsCombo_DropDown(object sender, System.EventArgs e)
{
//SPGlobalAdmin and SPVirtualServerCollection are under Microsoft.SharePoint.Administration
//add: using Microsoft.SharePoint.Administration; to the top top allow these to be easily accessible
//
SPGlobalAdmin myGlobalAdmin = new SPGlobalAdmin();
SPVirtualServerCollection SPVirtCol = myGlobalAdmin.VirtualServers;

this.vsList.Items.Clear();
this.vsCombo.Items.Clear();

foreach(Microsoft.SharePoint.Administration.SPVirtualServer SPVirt in SPVirtCol)
{
try
{
//Add the virtual server URLs to the visible listbox vsCombo.
//Add the virtualserver objects to vsList.
this.vsList.Items.Add(SPVirt);
this.vsCombo.Items.Add(SPVirt.Url);
}
catch(Exception ex)
{
errorBox.Items.Add(ex.Message);
}
}
}

private

void vsCombo_SelectedIndexChanged(object sender, System.EventArgs e)
{
//When you click the virutal server in the combo box, change the selected item in the hidden list.
vsList.SelectedIndex = vsCombo.SelectedIndex;
}

private

void setNewValues_Click(object sender, System.EventArgs e)
{
//I'm accessing the VirtualServer objects in the listbox by casting them as you see below.
SPSiteCollection SPSiteCol = ((SPVirtualServer)(vsList.SelectedItem)).Sites;
foreach(SPSite mySPSite in SPSiteCol)
{
if(mySPSite.Url == mySiteUrl)
{
//I am getting tempOldMax and tempOldWarn before changing them because...
//How else will I know its value when I go to log the success/failure?

//set max storage level
long tempOldMax = mySPSite.Quota.StorageMaximumLevel;
mySPSite.Quota.StorageMaximumLevel=Convert.ToInt64(newMax.Text);
errorBox.Items.Add(mySPSite.Url+" Old Max: "+tempOldMax+" New Max: "+newMax.Text);

//set warning storage level
long tempOldWarn = mySPSite.Quota.StorageWarningLevel;
mySPSite.Quota.StorageWarningLevel=Convert.ToInt64(newWarn.Text);
errorBox.Items.Add(mySPSite.Url+" Old Warn: "+tempOldWarn" New Warn: "+newWarn.Text);
}
}
}

Hope this is useful to someone... More entries to come next week. I'll be posting some SPList object code to show how to change metadata, and also some doclib code.