Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
EPiServer jobs to migrate Commerce assets between servers
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("\"", "\"\"") + '"'));
}
}
}
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();
}
}
}
}
@whyleee

This comment has been minimized.

Copy link
Owner Author

@whyleee whyleee commented Jul 23, 2015

To migrate EPiServer Commerce assets between servers, do next steps:

  1. Run "Export All Media Files" job on the source server. It will generate "ExportedMedia.episerverdata" file with exported assets and "MediaMappings.csv" file with content-asset mappings for AssetImporter.
  2. Copy "ExportedMedia.episerverdata" from the source server "App_Data" dir to the target server "App_Data" dir.
  3. Run "Import Media Files" job on the target server.
  4. Run AssetImporter to map content with assets: AssetImporter.exe MediaMappings.csv -- D:\Websites\MyWebsite -mappingonly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment