Fine-grained Data Restoration from Unattached Database
Last modified: November 22, 2009
Applies to: SharePoint Foundation 2010
This topic describes how the Microsoft SharePoint Foundation object model supports the creation of unattached databases from which granular data, down to the level of particular fields, can be restored to a SharePoint Foundation content database.
SharePoint Foundation supports highly granular data recovery by means of the unattached database feature. The process is simple. Your code creates an (unattached) SPContentDatabase object with a call to the static CreateUnattachedContentDatabase() method. Your code can then use the SharePoint Foundation object model to extract from the unattached database object site collections, Web sites, lists, list items, or fields in particular items. The data is then added to the appropriate parent object (or overwrites the appropriate target) in the target content database. Any of the following can serve as the source database:
A database snapshot. For more information about SharePoint Foundation support for database snapshots, see SPContentDatabase.
A shadow copy from the Volume Shadow Copy Service (VSS) that has been mounted in Microsoft SQL Server. For more information about SharePoint Foundation support for VSS, see SharePoint Foundation and the Volume Shadow Copy Service.
Any content database from the same major version of SharePoint Foundation that is running on an accessible instance of SQL Server.
Microsoft does not support creating an unattached database object from a content database of an earlier major version of SharePoint Foundation.
In the following example, a field in an Employees list specifies the ID number of a key to the building where the employees work. Specifically, it identifies the key that was issued to the employee. Consider a scenario in which this field was mistakenly changed for employee number 24. The following code resets the building key ID of employee number 24 back to what it was when a snapshot of the database was made. Assume that snap is an SPDatabaseSnapshot object that serves here as the source database.
// Create the unattached database from the source. SPContentDatabase unattachedDB = SPContentDatabase.CreateUnattachedContentDatabase(snap.ConnectionString); // Get the good data. SPList goodEmployeeList = unattachedDB.Sites["CustomSiteCol"].AllWebs["CustomSite"].Lists["Employees"]; SPListItemCollection goodItems = goodEmployeeList.GetItems(goodEmployeeList.DefaultView); SPListItem employeeGoodData = goodItems; Int32 keyNumber = employeeGoodData["BuildingKeyID"]; // Get a reference to the bad data. SPSite siteCol = SPContext.Current.Site; SPList badList = siteCol.AllWebs["CustomSite"].Lists["Employees"]; SPListItemCollection badItems = badList.GetItems(badList.DefaultView); SPListItem employeeBadData = badItems; // Overwrite the bad data. employeeBadData["BuildingKeyID"] = keyNumber; siteCol.ContentDatabase.Update();