How to perform export / import of site using API

   —   
The following article shows how to export or import your site using our API.   This artcle also shows how you can exclude certain objects from import process.
First step is to create ExportSite method, with the code below:
public static bool ExportSite(string siteName) { string websitePath = SettingsKeyProvider.WebApplicationPhysicalPath; string exportFileName = string.Format(siteName + "_{0:yyyy-MM-dd_hh-mm}.zip", DateTime.Now); // Check whether the site already exists if (SiteInfoProvider.GetSiteInfo(siteName) != null) { // Set export settings SiteExportSettings siteExportSettings = new SiteExportSettings(); siteExportSettings.ExportType = ExportTypeEnum.Site; siteExportSettings.SiteName = siteName; siteExportSettings.WebsitePath = SettingsKeyProvider.WebApplicationPhysicalPath; siteExportSettings.TargetPath = FileHelper.GetFullFolderPhysicalPath(ImportExportHelper.GetSiteUtilsFolder(), websitePath) + "Export\\"; siteExportSettings.CreatePackage = true; siteExportSettings.TargetFileName = exportFileName; siteExportSettings.DefaultProcessObjectType = ProcessObjectEnum.Selected; siteExportSettings.LoadDefaultSelection(); siteExportSettings.CopyFiles = false; // Export site ExportProvider.ExportObjectsData(siteExportSettings); return true; } return false; }

Second step would be to import previously exported site via ImportSite method:
public static bool ImportSite(string siteName) { string siteDisplayName = siteName; string siteDomain = "localhost"; string websitePath = SettingsKeyProvider.WebApplicationPhysicalPath; // Set import settings SiteImportSettings siteImportSettings = new SiteImportSettings(); siteImportSettings.ImportType = ImportTypeEnum.New; siteImportSettings.SiteDisplayName = siteDisplayName; siteImportSettings.SiteName = siteName; siteImportSettings.SiteDomain = siteDomain; siteImportSettings.SourceFilePath = @"C:\inetpub\wwwroot\KenticoCMS_7_newest\CMSSiteUtils\Import\CorporateSite_2014-02-23_03-06.zip"; siteImportSettings.DefaultProcessObjectType = ProcessObjectEnum.Selected; siteImportSettings.LoadDefaultSelection(); siteImportSettings.CopyFiles = false; // Exclude licences from import siteImportSettings.LoadDefaultSelection("cms.licensekey", false, ImportTypeEnum.None); // Import site ImportProvider.ImportObjectsData(siteImportSettings); return true; } }

The code of ImporSite method is more interesting then code of  first method, especially the following line:
siteImportSettings.LoadDefaultSelection("cms.licensekey", false, ImportTypeEnum.None);
That code simply tells the system that licences should be excluded from import process.  Basically you can exclude from import any object you want, all you have to do is to find its ClassName in CMS_Class database table.
Share this article on   LinkedIn