IBackupRestore.OnRestore method

Reads the backed up content and copies it to the target destination of the restore operation.

Namespace:  Microsoft.SharePoint.Administration.Backup
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

bool OnRestore(
	Object sender,
	SPRestoreInformation args
)

Parameters

sender
Type: System.Object

The object that called OnRestore.

args
Type: Microsoft.SharePoint.Administration.Backup.SPRestoreInformation

An SPRestoreInformation object that contains data about the operation.

Return value

Type: System.Boolean
true if successful; otherwise, false.

If your content class can be migrated, your code should check to see what the restore method is and call Rename() if the method is New.

If your content class has no content outside of any IBackupRestore child objects it may have, your implementation should simply set the CurrentProgess() to at least 50 percent and return true as seen in the following example. Do not call the OnRestore method of any IBackupRestore child objects.

public Boolean OnRestore(Object sender, SPRestoreInformation args)
{
    if (args == null)
    {
        throw new ArgumentNullException("args");
    }
    if (args.RestoreMethod == SPRestoreMethodType.New)
    {
        args.Rename();
    }

    args.CurrentProgress = 50;
    return true;
}

If your class does have content outside of any IBackupRestore child objects it may have, your implementation must copy this content to the restoration destination. Return false, if for any reason the copy of content fails.

The following example shows the overall structure of a substantive implementation of OnRestore():

public Boolean OnRestore(Object sender, SPRestoreInformation args)
{
    if (args == null)
    {
        throw new ArgumentNullException("args");
    }
    if (args.RestoreMethod == SPRestoreMethodType.New)
    {
        args.Rename();
    }

    args.CurrentProgress = 50;
    Boolean successSignal = true;

    // TODO: Implement copying your content to the destination.
    //       If the copy fails, set successSignal to false.

    return successSignal;
}

If a Windows service, or some application, needs to be stopped or paused during restoration, you can do so at the beginning of OnRestore. (Restart the service or application in OnPostRestore(Object, SPBackupInformation).) Do not do this work in OnPreRestore(Object, SPBackupInformation) The latter method is called for every component, even if it is not being restored; but OnBackupComplete is called only for components that are restored, so there is no guarantee that a service or application stopped in the prepare stage would get restarted.

The OnRestore method will not run if OnPreRestore returns false. If OnRestore returns false, the OnPostRestore method will not run.

The following example shows an implementation of OnRestore that restores files to a front end server. FrontEndFilePaths is a private field. It holds a collection of paths of the files that are to be restored.

public Boolean OnRestore(Object sender, SPRestoreInformation args)
{
    if (args == null)
    {
        throw new ArgumentNullException("args");
    }

    // If the CriticalFiles object was deleted from the farm after it was
    // backed up, restore it to the configuration database.
    CriticalFiles cf = SPFarm.Local.GetChild<CriticalFiles>(this.Name);
    if (cf == null)
    {
        this.Update();
        args.Log(SPBackupRestoreLogSeverity.Verbose, this.Name + " added back to configuration database.");
    }

    Boolean successSignal = true;

    // TODO: The following loop restores files to the local server. If there are 
    //       multiple front end servers, your code must iterate through all of 
    //       SPFarm.Local.Servers and restore the same files to every server whose
    //       Role property is SPServerRole.WebFrontEnd

    foreach (String path in FrontEndFilePaths)
    {
        FileInfo backupCopy = new FileInfo(path);
        FileInfo file = new FileInfo(args.Location + @"\" + backupCopy.Name);
        try
        {
            file.CopyTo(path, true);
            args.Log(SPBackupRestoreLogSeverity.Verbose, "Restored " + file.Name);
        }
        catch (Exception e)
        {
            args.Log(SPBackupRestoreLogSeverity.Verbose, file.Name + " not restored: " + e.Message);
            successSignal = false;
        }
    }
    
    args.CurrentProgress = 50;
    return successSignal;
}
Show: