共用方式為


IBackupRestore.OnRestore method

可讀取的備份的內容,並將其複製到目標目的地的還原作業。

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

Syntax

'宣告
Function OnRestore ( _
    sender As Object, _
    args As SPRestoreInformation _
) As Boolean
'用途
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
)

參數

傳回值

Type: System.Boolean
true如果成功 ;否則,請false。

備註

如果可以移轉內容的類別,您的程式碼應該檢查 restore 方法是什麼,如果方法New呼叫Rename()

如果您的內容類別不有可能會有任何IBackupRestore子物件以外的任何內容,實作應該只是將**CurrentProgess()**設為至少 50%,並傳回true ,如下列範例所示。不要呼叫任何IBackupRestore子物件的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;
    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

如果沒有在類別有可能會有任何IBackupRestore子物件以外的內容,您實作必須將此內容複製至還原的目的地。會傳回false,如果有任何原因失敗之複本的內容。

下列範例會顯示**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

如果 Windows 服務或某些應用程式,需要停止或暫停還原期間,您可以這麼OnRestore的開頭。(或重新啟動服務應用程式中OnPostRestore(Object, SPBackupInformation)。)不會做這項工作的**OnPreRestore(Object, SPBackupInformation)**後者方法會呼叫針對每個元件,即使它不所還原 ;但OnBackupComplete呼叫僅適用於還原的元件,所以在停止服務或應用程式不保證準備階段會取得重新啟動。

如果OnPreRestore傳回falseOnRestore方法不會執行。如果OnRestore傳回false, OnPostRestore方法不會執行。

Examples

下列範例會顯示OnRestore ,將檔案還原至前端伺服器的實作。FrontEndFilePaths是私人的欄位。它會保留所要還原的檔案路徑的集合。

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

請參閱

參照

IBackupRestore interface

IBackupRestore members

Microsoft.SharePoint.Administration.Backup namespace