public string ImportCatalogXml(string filePath) { CatalogImportExport cie = new CatalogImportExport(); cie.Import(File.OpenRead(filePath), AppContext.Current.ApplicationId, string.Empty, true); return("Catalog Imported"); }
private void ImportCatalogWithHandlers(string filePath, List <ICatalogImportHandler> catalogImportHandlers) { string originalFileName = Path.GetFileNameWithoutExtension(filePath); string filenameBeforePreImport = originalFileName + "-beforePreImport.xml"; XDocument catalogDoc = XDocument.Load(filePath); string directory = Path.GetDirectoryName(filePath) ?? ""; string completeFilePathToSave = Path.Combine(directory, filenameBeforePreImport); _logger.Debug($"Saving original file to {completeFilePathToSave}."); catalogDoc.Save(completeFilePathToSave); if (catalogImportHandlers.Any()) { foreach (ICatalogImportHandler handler in catalogImportHandlers) { try { _logger.Debug($"Preimport handler: {handler.GetType().FullName}"); handler.PreImport(catalogDoc); } catch (Exception e) { _logger.Error("Failed to run PreImport on " + handler.GetType().FullName, e); } } } var fs = new FileStream(filePath, FileMode.Create); catalogDoc.Save(fs); fs.Dispose(); var cie = new CatalogImportExport(); cie.ImportExportProgressMessage += ProgressHandler; cie.Import(directory, true); catalogDoc = XDocument.Load(filePath); if (catalogImportHandlers.Any()) { foreach (ICatalogImportHandler handler in catalogImportHandlers) { try { _logger.Debug($"Postimport handler: {handler.GetType().FullName}"); handler.PostImport(catalogDoc); } catch (Exception e) { _logger.Error("Failed to run PostImport on " + handler.GetType().FullName, e); } } } }
private void ImportCatalog(string path) { var cie = new CatalogImportExport(); cie.ImportExportProgressMessage += ProgressHandler; string directoryName = Path.GetDirectoryName(path); cie.Import(directoryName, true); }
private static void CreateCatalog(Stream file, string fileName) { if (file == null || fileName.IsNullOrEmpty()) { throw new Exception("File is required"); } var name = fileName.Substring(fileName.LastIndexOf("\\") == 0 ? 0 : fileName.LastIndexOf("\\") + 1); var path = HostingEnvironment.MapPath("~/App_Data/Catalog"); var zipFile = Path.Combine(path, name); var zipDirectory = new DirectoryInfo(Path.Combine(path, name.Replace(".zip", ""))); if (zipDirectory.Exists) { zipDirectory.Delete(true); } zipDirectory.Create(); var zipInputStream = new ZipFile(file) { IsStreamOwner = false }; foreach (ZipEntry zipEntry in zipInputStream) { if (!zipEntry.IsFile) { continue; } var entryFileName = zipEntry.Name; var zipStream = zipInputStream.GetInputStream(zipEntry); using (var fs = new FileStream(Path.Combine(zipDirectory.FullName, entryFileName), FileMode.Create, FileAccess.ReadWrite)) { zipStream.CopyTo(fs); } } var assests = zipDirectory.GetFiles("ProductAssets*") .FirstOrDefault(); var catalogXml = zipDirectory.GetFiles("*.xml") .FirstOrDefault(); if (catalogXml == null || assests == null) { throw new Exception("Zip does not contain catalog.xml or ProductAssets.episerverdata"); } var catalogFolder = _contentRepository.Value.GetChildren <ContentFolder>(ContentReference.GlobalBlockFolder) .FirstOrDefault(_ => _.Name.Equals("Catalogs")); if (catalogFolder == null) { catalogFolder = _contentRepository.Value.GetDefault <ContentFolder>(ContentReference.GlobalBlockFolder); catalogFolder.Name = "Catalogs"; _contentRepository.Value.Save(catalogFolder, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess); } EPiServer.Find.Cms.EventedIndexingSettings.Instance.EventedIndexingEnabled = false; EPiServer.Find.Cms.EventedIndexingSettings.Instance.ScheduledPageQueueEnabled = false; ImportEpiserverContent(assests.OpenRead(), catalogFolder.ContentLink, ServiceLocator.Current.GetInstance <IDataImporter>()); try { var catalogImportExport = new CatalogImportExport() { IsModelsAvailable = true }; catalogImportExport.Import(catalogXml.OpenRead(), true); } catch (Exception exception) { LogManager.GetLogger().Error(exception.Message, exception); } EPiServer.Find.Cms.EventedIndexingSettings.Instance.EventedIndexingEnabled = true; EPiServer.Find.Cms.EventedIndexingSettings.Instance.ScheduledPageQueueEnabled = true; }