|
1. Begin a new Class project in Visual Studio.
2. 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 your class file.
3. If your class does not derive from SPPersistedObject, implement the IBackupRestore.Name property. This will serve as the name of the content component in the UI of stsadm.exe, the Central Administration application and the UI of any custom backup and restore application. In most cases you implement the property by creating a private field for the name value and implement the public property as a wrapper around the field. For information on possible variant implementations, see the reference topic for the property.
[C#]
private String name;
public String Name
{
get {return name;}
set {name = value;}
}
4. If your class does not derive from SPPersistedObject, implement the IBackupRestore.Id property. In most cases, you implement the property by creating a private field for the name value and implement the public property as a wrapper around the field. For information on possible variant implementations, see the reference topic for the property.
[C#]
private Guid id;
public Guid Id
{
get {return id;}
set {id = value;}
}
5. Implement the IBackupRestore.DiskSizeRequired property. If your class is just a container for some child IBackupRestore classes, the property should return 0. Otherwise, the property should calculate the size of the content. (Include the size of any non-IBackupRestore child objects, but do not include the size of any child IBackupRestore objects. They each have their own IBackupRestore.DiskSizeRequired property and Windows SharePoint Server 3.0 will add those values in automatically.) The following example sums the sizes of all the files whose paths are contained in a collection called FrontEndFilePaths.
[C#]
public UInt64 DiskSizeRequired
{
get
{
UInt64 total = 0;
List<FileInfo> FrontEndFiles = new List<FileInfo>(NUMBER_OF_FILES_TO_BACK_UP);
foreach (String path in FrontEndFilePaths)
{
FileInfo file = new FileInfo(path);
FrontEndFiles.Add(file);
}
foreach (FileInfo file in FrontEndFiles)
{
total = total + (UInt64)file.Length;
}
return total;
}
}
6. Implement the IBackupRestore.CanSelectForBackup property. If users should never be able to backup objects of your class independently of a backup of the parent object, the get accessor should return false. If users should always be able to select any object of your class for independent backup, the get accessor should return true. In either case, the set accessor should be an empty pair of braces "{ }". If some objects of your class can be backed up independently of their parent, but some cannot be, implement the property as a wrapper around a private System.Boolean field.
7. Implement the IBackupRestore.CanSelectForRestore property. If users should never be able to restore objects of your custom component class independently of a restoration of the parent object, the get accessor should return false. If users should always be able to select any object of your class for independent restoration, the get accessor should return true. In either case, the set accessor should be an empty pair of braces "{ }". If some objects of your class can be restored independently of their parent, but some cannot be, implement the property as a wrapper around a private System.Boolean field.
8. Implement the IBackupRestore.CanRenameOnRestore property. If users should never be able to restore objects of your custom component class to a new location, the get accessor should return false. If users should be able to migrate any object of your class, the get accessor should return true. If objects of your class can sometimes be migrated, but not always, implement the property as a wrapper around a private System.Boolean field.
9. Implement the IBackupRestore.AddBackupObjects method.
a. Your implementation code should begin by throwing an exception if there is no valid parent to which the component can be added.
b. Use the SPBackupRestoreObject.AddChild method to add your component to the tree of objects that the backup or restore operation will process.
c. Use the SPBackupRestoreInformation.SetParameter method to specify a type name and description of the component that can be used by the UI of backup/restore applications.
d. If the component has child IBackupRestore objects, your implementation should iterate through them and recursively call the IBackupRestore.AddBackupObjects method of each child.
e. See the reference topic for the IBackupRestore.AddBackupObjects method for more ideas about implementations of it.
The following example code assumes that your content class has a ChildContentCollection of child IBackupRestore objects. If your class has more than one type of child component, you may have separate collections for each type and iterate through each collection.
[C#]
public void AddBackupObjects(SPBackupRestoreObject parent)
{
if (parent == null)
{
throw new ArgumentNullException("parent");
}
SPBackupRestoreObject self = parent.AddChild(this);
self.Information.SetParameter(SPBackupRestoreObject.SPTypeName, this.GetType());
self.Information.SetParameter(SPBackupRestoreObject.SPDescription,
"Description of custom content component");
foreach (ChildContent child in ChildContentCollection)
{
IBackupRestore childIBR = child as IBackupRestore;
childIBR.AddBackupObjects(self);
}
}
10. Implement the IBackupRestore.OnAbort method. It should always return true. In most cases it should do nothing more, but see the reference topic for IBackupRestore.OnAbort for information about exceptions to this general rule.
11. Implement the IBackupRestore.OnPrepareBackup method. At a minimum, you should use the SPBackupRestoreInformation.SetParameter method to specify a name for the content object. Beyond that, few generalizations can be made. See the reference topic for IBackupRestore.OnPrepareBackup for more information. The following example shows a minimal implementation of the method, which is often all that is needed.
[C#]
public Boolean OnPrepareBackup(Object sender, SPBackupInformation args)
{
if (args == null)
}
throw new ArgumentNullException("args");
}
args.SetParameter(SPBackupRestoreObject.SPName, this.Name);
return true;
}
12. Implement the IBackupRestore.OnBackup method. If your content class has no content outside of any IBackupRestore child objects it may have, your implementation should simply set the SPBackupRestoreInformation.CurrentProgess to a value that approximately represents the percentage of the total backup operation time that is consumed by the IBackupRestore.OnBackup and IBackupRestore.OnPrepareBackup methods. It should then return true as seen in the following example. Do not call the IBackupRestore.OnBackup method of any IBackupRestore child objects.
[C#]
public Boolean OnBackup(Object sender, SPBackupInformation args)
{
if (args == null)
{
throw new ArgumentNullException("args");
}
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 args.SPBackupRestoreInformation.Location and return false if the copy fails. You should include logic to backup any child objects that do not implement IBackupRestore, but you should not explicitly backup any child objects that do implement IBackupRestore. They will be backed up by their own IBackupRestore.OnBackup method, which the runtime will call. You should not call the IBackupRestore.OnBackup methods of the child objects in your own code. The following example shows the overall structure of a substantive implementation of IBackupRestore.OnBackup.
[C#]
public Boolean OnBackup(Object sender, SPBackupInformation args)
{
if (args == null)
{
throw new ArgumentNullException("args");
}
args.CurrentProgress = 50;
Boolean successSignal = true;
// TODO: Implement copying your content to args.Location
// If the copy fails, set successSignal to false.
return successSignal;
}
13. Implement the IBackupRestore.OnBackupComplete method. At a minimum, your implementation should set SPBackupRestoreInformation.CurrentProgess to 100 percent and return true as shown in the following example. This is typically all that is required. For information about other work your implementation may need to perform, see the reference topic for IBackupRestore.OnBackupComplete.
[C#]
public Boolean OnBackupComplete(Object sender, SPBackupInformation args)
{
if (args == null)
{
throw new ArgumentNullException("args");
}
args.CurrentProgress = 100;
return true;
}
14. Implement the IBackupRestore.OnPreRestore method. In most situations, a restoration operation requires no preparation and your implementation of IBackupRestore.OnPreRestore should just return true. For information about other work your implementation may need to perform, see the reference topic for IBackupRestore.OnPreRestore.
15. Implement the IBackupRestore.OnRestore method.
· If your content class can be migrated, your code should check to see what the restore method is and call SPBackupRestoreInformation.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 SPBackupRestoreInformation.CurrentProgess to a value that approximately represents the percentage of the total restore operation time that is consumed by the IBackupRestore.OnRestore and the IBackupRestore.OnPreRestore methods. It should then return true as seen in the following example. Do not call the IBackupRestore.OnRestore method of any IBackupRestore child objects.
[C#]
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 IBackupRestore.OnRestore:
[C#]
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;
}
16. Implement the IBackupRestore.OnPostRestore method. At a minimum, your implementation should set SPBackupRestoreInformation.CurrentProgess to 100 percent and return true as shown in the following example. This is typically all that is required. For information about other work your implementation may need to perform, see the reference topic for IBackupRestore.OnPostRestore.
[C#]
public Boolean OnPostRestore(Object sender, SPRestoreInformation args)
{
if (args == null)
{
throw new ArgumentNullException("args");
}
args.CurrentProgress = 100;
return true;
} |