Compartilhar via


Fine-grained Data Restoration from Unattached Database

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.

Fine-Grained Data Repair

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.

    Note

    Microsoft does not support creating an unattached database object from a content database of an earlier major version of SharePoint Foundation.

Warning

An unattached database must be treated as read-only. If you call the Update() method of an unattached database, an NotSupportedException is thrown.

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[24];
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[24];

// Overwrite the bad data.
employeeBadData["BuildingKeyID"] = keyNumber;
siteCol.ContentDatabase.Update();