No content databases are available for this operation when restoring a site

Recently one of my customers was seeing some odd behavior in SharePoint 2007 while attempting to restore a site. Each time the restore operation (STSADM –o restore) was run, the following error message would appear:

No content databases are available for this operation. Create a content database, and then try the operation again. To create a content database, click "Content databases" on the Application Management page, select the Web application to use, and then click "Add a content database".

Now this seems pretty straight forward. Except for on thing, the particular web application that the site collection was being restored to, had plenty of Content Databases (around 10) and most of them had plenty of room for new sites. The site quotas had not been hit for these content databases and none of the Content databases was locked or in read-only mode.

Looking at the ULS logs, we got more information about the error included in the stack trace, here is the full error in the ULS logs:

05/17/2010 14:57:22.18 STSADM.EXE (0x11C0) 0x19F0 Windows SharePoint Services General 72ju High stsadm: No content databases are available for this operation. Create a content database, and then try the operation again. To create a content database, click "Content databases" on the Application Management page, select the Web application to use, and then click "Add a content database". Callstack: at Microsoft.SharePoint.Administration.SPContentDatabaseCollection.FindBestContentDatabaseForSiteCreation(Guid siteIdToAvoid, Guid webIdToAvoid) at Microsoft.SharePoint.Administration.SPSiteCollection.Restore(String strSiteUrl, String strFilename, Boolean bOverwrite, Boolean hostHeaderAsSiteName) at Microsoft.SharePoint.StsAdmin.SPAdminRestore.RunSiteCollectionRestore(StringDictionary keyValues) at Microsoft.SharePoint.StsAdmin.SPAdminRestore.Run(StringDictionary keyValues) ...

05/17/2010 14:57:22.18* STSADM.EXE (0x11C0) 0x19F0 Windows SharePoint Services General 72ju High ... at Microsoft.SharePoint.StsAdmin.SPStsAdmin.RunOperation(SPGlobalAdmin globalAdmin, String strOperation, StringDictionary keyValues, SPParamCollection pars)

The part that tickled my brain was this bit:

Microsoft.SharePoint.Administration.SPContentDatabaseCollection .FindBestContentDatabaseForSiteCreation(Guid siteIdToAvoid, Guid webIdToAvoid)

FindBestContentDataBaseForSiteCreation tells us exactly what it is doing, the code at this point attempts to locate the best content database within the web application to restore the site collection to. The part to pay particular attention to, is that it takes two GUIDs when making this call, siteIdToAvoid and webIdToAvoid.

So assume that when this call is being made, the GUID from the site collection that you are attempting to restore is being used. Makes perfect sense, what we are doing is looking for a content database that does not already contain a site collection with this ID. Further investigation showed that this particular restore file, was being used in place of a template in order to create new sites based off of an existing one. The issue with this, is that backup/restore, maintains the Site Collection (siteIdToAvoid) and Subsite (webIdToAvoid) ID of the backed up site. You cannot restore the same site to the same content database more than once unless you overwrite or delete the existing instance. So using a backup as a template for creating new sites, will not work more than once per content database.

So in our case, the site had been restored enough times so that each content database in the web application already had an instance of the site, which is why the above error message was being displayed. Though a bit misleading depending on how you read it, it is actually an accurate error message.

If you really need to take an existing site, and create other sites based on it, save the site as a template. And use STSADM to add that template to the gallery. You can then create new sites based on it. There are of course some limitations with templates as well, but they are pretty straight forward. Here are some references for site templates:

Note that these are both direct from the Site Actions in the Browser. In order to add a site so that it can be globally available, you need to use STSADM to add the template to SharePoint.