Dans cet article

How to: Programmatically Restore Content

05/05/2014

6 minutes de lecture

Dans cet article

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

To Restore a Content Component

Add a reference to Windows SharePoint Services to your Visual Studio project and add using statements for the Microsoft.SharePoint.Administration and Microsoft.SharePoint.Administration.Backup namespaces to you code file.

Prompt the user to specify the content component that is to be restored and assign its name to the IndividualItem property. To see an itemization of the names of the components on your farm that were included in the last full backup and that can be the objects of restore operations, you can either run the command stsadm -o restore -showtree at the server command line. To specify a different full backup package, use the -backupid parameter. Alternatively, you can visit Operations > Perform a Restore in the Central Administration application. To specify the whole farm, use "Farm" as the name. (Setting the property to null also selects the whole farm for backup assuming that you use IndividualItem in all subsequent code to identify by name the component to be restored, as you should. For an example, see the use of the FindItems(Guid, String) method in step 9.)

Console.Write("Enter name of component to restore (default is whole farm):");
settings.IndividualItem = Console.ReadLine();

If you want to restore from a backup other than the most recent, identify the backup package by assigning its GUID to the BackupId property. A record of each backup operation for a particular backup location is stored in spbrtoc.xml in the root of the location. Each backup and restore operation is represented in the file by an <SPHistoryObject> element. If the operation is a backup, the <IsBackup> child of the <SPHistoryObject> element is "True". The <SPId> element of the <SPHistoryObject> element contains the GUID of the backup.

Add the following implementation of your EnsureUniqueValidComponentName method. Use the FindItems(Guid, String) method to retrieve a collection of content objects whose names match the user-entered name. If there is no match, prompt the user to try again. If there is more than one, prompt the user to be more specific. If the component name that the user entered is valid and not ambiguous, get a reference to the SPBackupRestoreObject object that represents the component that the user wants to restore.

private static SPBackupRestoreObject EnsureUniqueValidComponentName(SPBackupRestoreSettings settings, ref Guid operationGUID)
{
SPBackupRestoreObjectCollection list = SPBackupRestoreConsole.FindItems(operationGUID, settings.IndividualItem);
SPBackupRestoreObject component = null;
if (list.Count <= 0)
{
Console.WriteLine("There is no component with that name. Run again with a new name.");
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
}
else if (list.Count > 1) // The component name specified is ambiguous. Prompt user to be more specific.
{
Console.WriteLine("More than one component matches the name you entered.");
Console.WriteLine("Run again with one of the following:");
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("\t{0}", list[i].ToString());
}
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
}
else
{
component = list[0];
}
return component;
}

In the Main method, create a conditional structure that will run only if your EnsureUniqueValidComponentName method has returned a valid node.

if (node != null)
{
// TODO: Set the restore operation as the active operation
// and run it.
}

Replace the "TODO" line in the previous step with the following code. This sets the operation to be the active operation with the SetActive(Guid) method and tests to verify that it succeeded. If it fails, which it will if another backup or restore operation is already underway, report an error to the UI of your application.

if (SPBackupRestoreConsole.SetActive(restore) == true)
{
// TODO: Run the operation. See next step.
}
else
{
// Report through your UI that another backup
// or restore operation is underway.
Console.WriteLine("Another backup or restore operation is already underway. Try again when it ends.");
}

In the code branch that runs if the SetActive(Guid) call succeeds, run the operation with the Run(Guid, Backup, SPBackupRestoreObject) method. Test that the operation succeeds. If it fails, report the operation's failure message to your UI. The following code replaces the "TODO" line in the previous step.

Example

The following code shows how to program a restoration of a content component. Replace the placeholder \\Server\WSSBackups with the path of your backup location. The runtime will automatically find the most recent backup at that location.