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)

Syntax

'Declaration
Function OnRestore ( _
    sender As Object, _
    args As SPRestoreInformation _
) As Boolean
'Usage
Dim instance As IBackupRestore
Dim sender As Object
Dim args As SPRestoreInformation
Dim returnValue As Boolean

returnValue = instance.OnRestore(sender, _
    args)
bool OnRestore(
    Object sender,
    SPRestoreInformation args
)

Parameters

Return Value

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

Remarks

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;
}
Public Function OnRestore(ByVal sender As Object, ByVal args As SPRestoreInformation) As Boolean
    If args Is Nothing Then
        Throw New ArgumentNullException("args")
    End If
    If args.RestoreMethod = SPRestoreMethodType.New Then
        args.Rename()
    End If

    args.CurrentProgress = 50
    Return True
End Function

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;
}
Public Function OnRestore(ByVal sender As Object, ByVal args As SPRestoreInformation) As Boolean
    If args Is Nothing Then
        Throw New ArgumentNullException("args")
    End If
    If args.RestoreMethod = SPRestoreMethodType.New Then
        args.Rename()
    End If

    args.CurrentProgress = 50
    Dim successSignal As Boolean = True

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

    Return successSignal
End Function

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.

Examples

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;
}
Public Function OnRestore(ByVal sender As Object, ByVal args As SPRestoreInformation) As Boolean
    If args Is Nothing Then
        Throw New ArgumentNullException("args")
    End If

    ' If the CriticalFiles object was deleted from the farm after it was
    ' backed up, restore it to the configuration database.
    Dim cf As CriticalFiles = SPFarm.Local.GetChild(Of CriticalFiles)(Me.Name)
    If cf Is Nothing Then
        Me.Update()
        args.Log(SPBackupRestoreLogSeverity.Verbose, Me.Name & " added back to configuration database.")
    End If

    Dim successSignal As Boolean = 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

    For Each path As String In FrontEndFilePaths
        Dim backupCopy As New FileInfo(path)
        Dim file As New FileInfo(args.Location & "\" & backupCopy.Name)
        Try
            file.CopyTo(path, True)
            args.Log(SPBackupRestoreLogSeverity.Verbose, "Restored " & file.Name)
        Catch e As Exception
            args.Log(SPBackupRestoreLogSeverity.Verbose, file.Name & " not restored: " & e.Message)
            successSignal = False
        End Try
    Next path

    args.CurrentProgress = 50
    Return successSignal
End Function

See Also

Reference

IBackupRestore Interface

IBackupRestore Members

Microsoft.SharePoint.Administration.Backup Namespace