SPBackupRestoreInformation.GenerateFileMapping Method

Generates a unique name for the backup file that contains the specified source file.

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

Syntax

'Declaration
Public Function GenerateFileMapping ( _
    name As String _
) As String
'Usage
Dim instance As SPBackupRestoreInformation
Dim name As String
Dim returnValue As String

returnValue = instance.GenerateFileMapping(name)
public string GenerateFileMapping(
    string name
)

Parameters

Return Value

Type: System.String
A String that represents the name of the backup file that contains the backed up copy of name.

Remarks

This method is valuable when a backup operation contains multiple files with the same name. To ensure that each is separately backed up instead of overwriting each other, this method can be used to generate a unique name for each. It is typically called in implementations of OnBackup(). See the example below.

The format of the return value is hex.bak, where hex is an eight digit hexadecimal number; for example, "00000001.bak" or 0000000A.bak".

The name parameter also becomes a key in a store of key-value pairs, and the string returned by GenerateFileMapping becomes the value of the key. This pair is stored in the spbackup.xml file in the Location folder.

Note

Although GenerateFileMapping and ReverseFileMapping return exactly the same value in response to the same input, they are doing different things internally. GenerateFileMapping creates the unique file name and writes the mapped pair of file names to the spbackup.xml file. ReverseFileMapping reads the mapping in that file.

Examples

The following example shows the GenerateFileMapping method used in an implementation of OnBackup(). For the full example, see How to: Create a Content Class That Can Be Backed Up and Restored.

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

    Boolean successSignal = true;

    foreach (String path in FrontEndFilePaths)
    {
        FileInfo file = new FileInfo(path);
        try
        {
            String mappedFileName = args.GenerateFileMapping(file.Name);
            file.CopyTo(args.Location + @"\" + mappedFileName, true);
            args.Log(SPBackupRestoreLogSeverity.Verbose, "Backed up " + file.Name + " (in " + mappedFileName + ")");
        }
        catch (Exception e)
        {
            args.Log(SPBackupRestoreLogSeverity.Verbose, file.Name + " not backed up: " + e.Message);
            successSignal = false;
        }
    }

    args.CurrentProgress = 50;
    return successSignal;
}
Public Function OnBackup(ByVal sender As Object, ByVal args As SPBackupInformation) As Boolean
    If args Is Nothing Then
    Throw New ArgumentNullException("args")
    End If

    Dim successSignal As Boolean = True

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

    args.CurrentProgress = 50
    Return successSignal
End Function

See Also

Reference

SPBackupRestoreInformation Class

SPBackupRestoreInformation Members

Microsoft.SharePoint.Administration.Backup Namespace

ReverseFileMapping