Content Events and Service API

Vote:
 

Hi all

I have created a small initialization module, that reacts to the PublishedContent event and does some stuff.
It works perfectly when manually updating content in the catalog, but when the data is updated via Service API, it seems like the PublishedContent is not raised, is that correct?

#267724
Edited, Dec 02, 2021 9:16
Vote:
 

Yes, thats correct from my experience also.

Publishing products through Service API does not raise the Content Publishing Event.

Creating Entry's through the IContentRepository API will trigger ContentEvents however. You may need to create a custom API to save products if you need the Content Event triggered. 

#267726
Edited, Dec 02, 2021 12:05
Vote:
 

It depends on your version. If you are using the latest (or rather 5.2 or later IIRC), then ServiceAPI will fire content events.

#267727
Dec 02, 2021 12:39
Johnny Mullaney - Dec 02, 2021 12:46
Didn't know this feature is now included. Thank you
Vote:
 

We are using v3.0.1 due to the commerce version - what is the alternative then? It's simply not possible to detect a publish via Service API then?

#267731
Dec 02, 2021 13:13
Vote:
 

You can listen to the low level event. EventContext.EntryUpdated. the CatalogEntryDto is only updated if an entry is created or "published", so it's safe to assume. However it only works locally. If you have a separate ServiceAPI instance, it will be tricky 

#267733
Dec 02, 2021 13:25
Vote:
 

Great, I will give it a try, and let you know if it works. I also need to delete an entry in a Solr index, when a Variation is deleted in the catalog. If I use "EntryDeleted", the Variation has already been deleted, and I cannot retrieve the Code. Is there an "EntryDeleting"?

#267742
Dec 02, 2021 20:38
Vote:
 

I can confirm, that it works with EventContext.EntryUpdated - Thanks a lot!

Now I need to figure out, how I retrieve the code from a deleted variation, can you help?

#267777
Dec 03, 2021 9:13
Vote:
 

Unfortunately there is no EntryDeleting event. I'm not sure why, probably just an overlook. A workaround is to listen to EntryUpdating event, then look into the sender (CatalogEntryDto). You can see if there is any deleted row in there, and use DataRowVersion to get the deleted code.

#267778
Dec 03, 2021 9:30
Vote:
 

Could you give an example of how to do this?

#267779
Dec 03, 2021 10:05
Vote:
 
#267976
Dec 07, 2021 14:35
Vote:
 

Quan Mai, thanks a lot for the help! I can confirm that it works.
Here is the code I ended up with, I'm only acting on "Variation". Also I extended the CatalogEventListenerBase.

public override void EntryUpdating(object source, EntryEventArgs args)
{
    base.EntryUpdating(source, args);

    var entry = source as CatalogEntryDto;
    if (entry != null)
    {
        var rows = entry.CatalogEntry.Where(x => x.RowState == DataRowState.Deleted);
        var deletingVariationCodes = rows.Where(x => (string)x["ClassTypeId", DataRowVersion.Original] == "Variation").Select(x => (string)x["Code", DataRowVersion.Original]);
        foreach(var code in deletingVariationCodes)
        {
            // Update the variants in the index
        }
    }
}
#268031
Dec 08, 2021 8:11
* 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.