Tuesday, December 28, 2010

Exporting SharePoint web and Importing to same SiteCollection

There is built in feature in stsadm to Export and Import sites and webs, It is very useful and almost always sufficient, but there can be more complex scenarios when it is just not enough then there is way to do this in code.
private static string Export(string siteURL, string fileToExport)
{
  SPExportSettings exportSettings = new SPExportSettings();
  exportSettings.AutoGenerateDataFileName = true;
  exportSettings.ExportMethod = SPExportMethodType.ExportAll;
  exportSettings.SiteUrl = siteURL;
  exportSettings.IncludeSecurity = SPIncludeSecurity.None;
  exportSettings.IncludeVersions = SPIncludeVersions.CurrentVersion;
  exportSettings.CommandLineVerbose = true;
  exportSettings.FileLocation = fileToExport;
 
  SPExportObject exportObject = new SPExportObject();
  exportObject.ExcludeChildren = false;
  exportObject.Type = SPDeploymentObjectType.Web;
 
  using (SPSite site = new SPSite(siteURL))
  {
    using (SPWeb web = site.OpenWeb(siteURL.Substring(site.Url.Length, siteURL.Length - site.Url.Length)))
    {
      exportObject.Id = web.ID;
    }
  }
  exportObject.IncludeDescendants = SPIncludeDescendants.All;
  exportSettings.ExportObjects.Add(exportObject);
  SPExport export = new SPExport(exportSettings);
  export.Run();
  return exportSettings.FileLocation + "\\" + exportSettings.BaseFileName;
}
private static void Import(string siteURL, string fileToImport)
{
  SPImportSettings importSettings = new SPImportSettings();
  importSettings.BaseFileName = System.IO.Path.GetFileName(fileToImport);
  importSettings.FileLocation = System.IO.Path.GetDirectoryName(fileToImport);
  importSettings.SiteUrl = siteURL;
  importSettings.RetainObjectIdentity = false;
  importSettings.IncludeSecurity = SPIncludeSecurity.None;
  importSettings.UpdateVersions = SPUpdateVersions.Ignore;
  importSettings.UserInfoDateTime = SPImportUserInfoDateTimeOption.None;
  importSettings.SuppressAfterEvents = true;
  importSettings.CommandLineVerbose = true;
  SPImport import = new SPImport(importSettings);
  import.Run();
}
 
Continiunes (Part 2) 

No comments:

Post a Comment