Creating new content in events triggered by import process might cause errors

Found in

EPiServer.CMS.Core 7.5.394.2

Fixed in

EPiServer.CMS.Core 11.1.0

Created

Jun 29, 2017

Updated

Nov 21, 2017

Area

CMS Core

State

Closed, Fixed and tested


Description

If a new content item is created with ImportContext, e.g.' by attaching an event handler to IDataImportEvents.ContentImported and then, in the event handler, something like this happens

var page = contentRepository.GetDefault<ContentFolder>(ContentReference.RootPage);
page.Name = "something";
contentRepository.Save(page);

an exception is thrown PropertyDate is out of range. Example: [Importing content 6202_8055] Exception: You must enter a value between 01/01/1753 and 12/31/9999 for "EPiServer.Core.PropertyDate".

Note: This is because the DataImporter sets a context key ContextCache.Current[UseIChangeTrackingSavedKey]. When that is set, it does not set the saved date to DateTime.Now but instead uses the date from the content itself. This is so the content gets the same time at it has in import package. While this works for content in the package, if other content is created (e.g., from an event handler), it will fail. We should use DateTime.Now for those content items instead of throwing an exception.