Importing the test node package that we exported earlier (see article Exporting Episerver content programmatically through an ApiController) would be the next step in our automated transfer of the production database to the testing environment. I extended our ApiController with an import endpoint for this purpose.
Note that this code is never intended to be deployed to production.
[HttpPut]
[Route("v1/import-node")]
public IHttpActionResult ImportTestNode()
{
var request = HttpContext.Current.Request;
_importExportService.ImportTestNode(request.InputStream);
return Ok();
// Error handling removed to reduce noise.
}
Importing is easier than exporting. The relevant Episerver interface IDataImporter is not disposable. We will still use the ServiceLocator to get an instance as the default implementation may cause issues if we were to reuse it in our singleton ImportExportService.
There are a lot of options you can specify on the ImportOptions object, but for our intentions the default ones work just fine.
public class ImportExportService
{
// ...
internal void ImportTestNode(Stream inputStream)
{
ImportOptions options = ImportOptions.DefaultOptions;
options.AutoCloseStream = true; // We do not need this afterwards, so just keep it true.
IDataImporter dataImporter = ServiceLocator.Current.GetInstance<IDataImporter>();
_ = dataImporter.Import(inputStream, destinationRoot: ContentReference.StartPage, options);
}
This will import the package being sent in the PUT request body to a node under the current StartPage. In order to make the previously mentioned export endpoint get the proper node, we would need to update information about this somewhere. In our case, we keep this information in a PageReference property on our settings page. Please see Getting the root PageReference of the imported pages in Episerver for how to update it.