June 13, 2013

Backup/Restore SPWeb programmatically

  • Backup SPWeb Many times we may require to backup SPWeb programmatically. This is the method that can be used to generate backup file of SPWeb. This will generate backup file with ".cmp" extensions and will be stored at specified location.

    //This will create backup file (ABC.cmp) of SPWeb "ABC".
    using (SPSite site = new SPSite("http://br76:42319/ABC"))
    using (SPWeb web = site.OpenWeb())
    string bPath = "C:\\Backup"; //Location where backup file is to be stored.
    SPExportObject exportSite = new SPExportObject();
    exportSite.Type = SPDeploymentObjectType.Web;
    exportSite.Url = web.Url;
    exportSite.IncludeDescendants = SPIncludeDescendants.All;
    SPExportSettings settings = new SPExportSettings();
    settings.AutoGenerateDataFileName = false;
    settings.BaseFileName = "ABC"; // Name of the backup file.
    settings.FileLocation = bPath;
    settings.SiteUrl = web.Site.Url;
    settings.ExportMethod = SPExportMethodType.ExportAll;
    settings.FileCompression = true;
    settings.IncludeSecurity = SPIncludeSecurity.All;
    settings.IncludeVersions = SPIncludeVersions.All;
    settings.ExcludeDependencies = false;
    settings.OverwriteExistingDataFile = true;
    SPExport spExport = new SPExport(settings);
  • Restore SPWeb
    This method can be used to restore SPWeb using the backup file of SPWeb.

    //This will create restore web "ABC" from backup file "ABC.cmp".
    using (SPSite site = new SPSite("http://br76:42319/ABC"))
    using (SPWeb web = site.OpenWeb())
    SPImportSettings settings = new SPImportSettings();
    settings.SiteUrl = web.Site.Url;
    settings.FileLocation = "C:\\Backup";//Location where backup file is located.
    settings.BaseFileName = "ABC.cmp";// Name of the backup file.
    settings.UpdateVersions = SPUpdateVersions.Overwrite;
    settings.RetainObjectIdentity = false;
    SPImport import = new SPImport(settings);

If you have any questions you can reach out our SharePoint Consulting team here.

1 comment:

  1. HI How can i restore cmp file to From http://serv/ABC to http://serv/XYZ