Access to content item Version History via API

Is there an API or other way to get the Version History data for a content item within the code? I see in the Kentico Admin the “Show version history” button but I’m wondering if I can get access to that to display it elsewhere.


Environment

  • Xperience by Kentico version: 31.8.4
  • .NET version: 10

Answers

Hi Chorst,

As far as I know, there's no public API to read version history in 31.8.4. The only versioning type in CMS.ContentEngine is ContentItemVersionActionContext, and that's for disabling version storing, not reading versions. The history is stored in the database, but that storage isn't documented, so I wouldn't rely on it because it could change in any Refresh.

What works well is recording the history yourself with content events, available since 31.3:

public class PagePublishHistoryHandler : IAsyncEventHandler<AfterPublishWebPageEvent>
{
    public Task HandleAsync(AfterPublishWebPageEvent asyncEvent, CancellationToken cancellationToken)
    {
        var data = asyncEvent.Data;
        // store data.ContentTypeName, data.TreePath, data.WebsiteChannelName, timestamp...
        return Task.CompletedTask;
    }
}

Register it in a module's OnPreInit:

parameters.Services.AddEventHandler<AfterPublishWebPageEvent, PagePublishHistoryHandler>();

For Content hub items use AfterPublishContentItemEvent, and for headless items AfterPublishHeadlessItemEvent.

I tested this on Dancing Goat (31.8.4), and it fired for both pages and Content hub items. The built-in history also only records published versions (saves don't create entries), so this approach gives you at least the same data. Just keep in mind that it only captures publishes from the moment you add it.

Docs: Handle content events

To response this discussion, you have to login first.