World is now on Opti ID! Learn more

Cannot programmatically set created date for a page

Vote:
 

Hi developers,

Posting here, hoping to get some help.
I’m encountering an issue with setting the created date programmatically for page content. When copying a page, the Created and Published Dates are retained from the original page, but I expect these dates to update to the time when the page is copied.
I tried using the CreatedContent event of IContentEvents, but it didn’t work. Has anyone experienced this issue before?

[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]

 

public class MyContentEvents : IInitializableModule
{
    private IContentEvents? _contentEvents;
 
    public void Initialize(InitializationEngine context)
    {
        _contentEvents = context.Locate.Advanced.GetInstance<IContentEvents>();
        _contentEvents.CreatedContent += _contentEvents_CreatedContent;
    }
 
    private void _contentEvents_CreatedContent(object? sender, ContentEventArgs e)
    {
        if (e.Content is PageData page)
        {
            var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
            var pageData = page.CreateWritableClone();
            pageData.SetValue("PageCreated", DateTime.UtcNow);
            contentRepository.Save(pageData, SaveAction.ForceCurrentVersion, AccessLevel.NoAccess);
        }
    }
 
    public void Uninitialize() { }
}
#339487
Jun 19, 2025 0:54
Vote:
 

Hi Tran

If you use the IContentRepository.Copy method, or if the user is copying in the CMS UI, then all the properties is copied as-is. It is the same as expoting and importing the page.

You will not be able to catch those copy actions specifially with the IContentEvents methods.

If you are going to copy the pages programmatically, then you can just clone and publish the page.

#339489
Jun 19, 2025 6:44
Vote:
 

Or if you want to "get your hands dirty", you could try to replace the EPiServer.Core.Transfer.Internal.ChangeTrackablePropertyTransform. Here you can try to override ImportProperty. If the property name is PageCreated you can put your own timestamp.

But it would also apply to everything else that is imported. So you could maybe limit it to some known destination or something.

#339490
Jun 19, 2025 6:47
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.