[KØ] Get last edited content from Optimizely CMS

There are several scenarios where you might want to retrieve the most recently updated blocks and pages from Optimizely CMS:

The code example below creates a query against the Activity Log (the same one you’ll find in the Optimizely CMS Settings UI). It retrieves all content that has been published (including edits that were published) within the last day.

Since the Activity Log may contain multiple events for the same content item, the query selects only the distinct content IDs. From there, you can process the content however you like.

var query = new ActivityQuery
{
    Action = (int)ContentActionType.Publish,
    ActivityType = "Content",
    Order = ActivityOrder.AsRecorded,
    CreatedAfter = DateTime.Now - TimeSpan.FromDays(1),
};

var activities = _activityQueryService.ListActivitiesAsync(query).GetAwaiter().GetResult()
    .Cast<ContentPublishActivity>()
    .DistinctBy(x => x.ContentLink.ID)
    .Select(x => x)
    .ToList();

foreach (var activity in activities)
{
    var content = _contentLoader.Get<IContent>(activity.ContentLink);
    // Do something with content updated during the last 24 hours
}

That's it!

Found this post helpful? Help keep this blog ad-free by buying me a coffee! ☕