Created
July 23, 2015 15:01
-
-
Save whyleee/fcbff840109fc040682f to your computer and use it in GitHub Desktop.
EPiServer jobs to migrate Commerce assets between servers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Web.Hosting; | |
using EPiServer; | |
using EPiServer.BaseLibrary.Scheduling; | |
using EPiServer.Commerce.Catalog.ContentTypes; | |
using EPiServer.Core; | |
using EPiServer.Core.Transfer; | |
using EPiServer.Enterprise; | |
using EPiServer.PlugIn; | |
using EPiServer.ServiceLocation; | |
using EPiServer.Web; | |
using Mediachase.Commerce.Catalog; | |
namespace MyWebsite.Jobs | |
{ | |
[ScheduledPlugIn(DisplayName = "Export All Media Files", | |
Description = "Exports all media files to \"App_Data\" folder.")] | |
public class ExportMediaJob : JobBase | |
{ | |
public Injected<IContentRepository> Repo { get; set; } | |
public Injected<ReferenceConverter> Refs { get; set; } | |
public override string Execute() | |
{ | |
GenerateDataFile(); | |
GenerateMappingFile(); | |
return "All media files and csv mapping file are exported to \"App_Data\" folder."; | |
} | |
private void GenerateDataFile() | |
{ | |
var outFilePath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "App_Data", "ExportedMedia.episerverdata"); | |
using (var outFileStream = new FileStream(outFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) | |
{ | |
using (var exporter = new DataExporter()) | |
{ | |
exporter.Stream = outFileStream; | |
var rootLink = SiteDefinition.Empty.GlobalAssetsRoot; | |
exporter.SourceRoots.Add(new ExportSource(rootLink, int.MaxValue)); | |
exporter.Export(); | |
} | |
} | |
} | |
private void GenerateMappingFile() | |
{ | |
var rootLink = Refs.Service.GetRootLink(); | |
var allLinks = Repo.Service.GetDescendents(rootLink); | |
var mappings = new Dictionary<string, string>(); | |
foreach (var link in allLinks) | |
{ | |
var entry = Repo.Service.Get<CatalogContentBase>(link) as EntryContentBase; | |
if (entry != null && entry.CommerceMediaCollection != null && entry.CommerceMediaCollection.Any()) | |
{ | |
var assetLink = entry.CommerceMediaCollection.First().AssetLink; | |
var asset = Repo.Service.Get<IContent>(assetLink); | |
if (!mappings.ContainsKey(entry.Code)) | |
{ | |
mappings.Add(entry.Code, asset.Name); | |
} | |
} | |
} | |
var csv = SerializeToCsv(mappings); | |
var outFilePath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "App_Data", "MediaMappings.csv"); | |
File.WriteAllText(outFilePath, csv); | |
} | |
private string SerializeToCsv(IDictionary<string, string> mappings) | |
{ | |
var csv = new StringBuilder(); | |
csv.AppendLine(GetCsvLine(new[] {"Entry Code", "File Name"})); | |
foreach (var mapping in mappings) | |
{ | |
csv.AppendLine(GetCsvLine(new[] { mapping.Key, mapping.Value })); | |
} | |
return csv.ToString(); | |
} | |
private string GetCsvLine(IEnumerable<string> values) | |
{ | |
return string.Join(",", values.Select(v => '"' + v.Replace("\"", "\"\"") + '"')); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Web.Hosting; | |
using EPiServer.BaseLibrary.Scheduling; | |
using EPiServer.Enterprise; | |
using EPiServer.PlugIn; | |
using EPiServer.Web; | |
namespace MyWebsite.Jobs | |
{ | |
[ScheduledPlugIn(DisplayName = "Import Media Files", | |
Description = "Imports media files from \"App_Data\\ExportedMedia.episerverdata\" file.")] | |
public class ImportMediaJob : JobBase | |
{ | |
public override string Execute() | |
{ | |
ImportMedia(); | |
return "Media files were successfully imported."; | |
} | |
private void ImportMedia() | |
{ | |
var importFilePath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "App_Data", "ExportedMedia.episerverdata"); | |
using (var importFileStream = new FileStream(importFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) | |
{ | |
var importer = new DataImporter | |
{ | |
Stream = importFileStream, | |
DestinationRoot = SiteDefinition.Empty.GlobalAssetsRoot | |
}; | |
importer.Import(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To migrate EPiServer Commerce assets between servers, do next steps:
AssetImporter.exe MediaMappings.csv -- D:\Websites\MyWebsite -mappingonly
.