I got the question here Content Events and Service API | Optimizely Developer Community , and CHKN contacted Optimizely developer support service which is the good/right thing to do. However it could be beneficial for a wider audience, hence this blogpost.
If you are developing new Optimizely Commerce Cloud now, you should be using the latest version (CMS 12/ Commerce 14), or at least Commerce 13.31 if you have to stay with .NET 4.8. You then could use the IContentEvents
to listen to any events that might be fired from Service API.
However, if you are using older versions, you might be limited to lower level, non content event through EventContext
. It works, but with a catch: there is no EntryDeleting
event in EventContext
. At this point I’m not entirely sure why, probably just an overlook. But it’s not impossible to work around that issue.
As I suggested in the post, EntryUpdating
is like a blanket event – every change to the entries goes through there. The sender is a CatalogEntryDto
, which should contain information about the being deleted entries.
private void Instance_EntryUpdating(object sender, Mediachase.Commerce.Catalog.Events.EntryEventArgs e)
{
var dto = sender as CatalogEntryDto;
if (dto != null)
{
var rows = dto.CatalogEntry.Where(x => x.RowState == DataRowState.Deleted);
var deletingCodes = rows.Select(x => (string)x["Code", DataRowVersion.Original]);
//do stuffs with codes being deleted.
}
}
And then to listen to the event in one of your IInitializationModule
EventContext.Instance.EntryUpdating += Instance_EntryUpdating;
However, there is a caveat here: as EntryUpdating
happens before the entry deletion, it is possible that the change did not go through (i.e. the changes were to be reverted). It is unlikely, but it’s a possibility. You might accept that, or you can:
- Store the id and code in a “Deleting entry” dictionary
- Listen to
EntryDeleted
event and match fromDeletedEntryEventArgs
parameter (which containsEntryId
) to get the deleted Code, and continue from there.