Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ayo Adesina 390 posts 967 karma points
    Jan 17, 2020 @ 15:44
    Ayo Adesina
    0

    Umbraco Events - getting newly created node inside the ContentService.Saved event

    This is the first time I'm using Umbraco events in Umbraco 8.

    I have subscribed to the ContentService.Saved event and this is firing correctly.

    However when a new node is created when I try to use the umbraco helper to retrive it as IPublished content, but I always get null. Why is this?

    As I understand it this event fires AFTER the new node has been saved, so I don't understand why I get NULL.

    Here is my code, any ideas?

    public class SubscribeToSaveEventComposer : ComponentComposer<SubscribeToSaveEventComponent>
    { }
    
    public class SubscribeToSaveEventComponent : IComponent
    {
        public void Initialize()
        {
            ContentService.Saved += ContentService_Saved;
            ContentService.Saving += ContentService_Saving;
        }
    
        private void ContentService_Saved(IContentService sender, ContentSavedEventArgs e)
        {
            foreach (var node in e.SavedEntities)
            {
                if (node.ContentType.Alias == "clientContainer")
                {   
                     //Why is Brand New Node = NULL
                    var brandNewNode = Umbraco.Web.Composing.Current.UmbracoHelper.Content(node.Id);
    
                   .........
                }
            }
        }
    
  • Malthe Petersen 24 posts 158 karma points c-trib
    Jan 17, 2020 @ 16:00
    Malthe Petersen
    0

    Hi.

    It is because you try to get something from the cache, which is not yet there.

    If you want that, you have to listen to CacheRefreserBase

  • Ayo Adesina 390 posts 967 karma points
    Jan 17, 2020 @ 16:08
    Ayo Adesina
    0

    when you mean listen to CacheRefreserBase what do you mean? I googled that and actually got 0 results lol

  • Malthe Petersen 24 posts 158 karma points c-trib
    Jan 17, 2020 @ 16:28
    Malthe Petersen
    0

    It is not yet documented,

    Instead if ContentService.Saving/Saved you have to listen to CacheRefresherBase<ContentCacheRefresher>.CacheUpdated and now you should be able to get the newly created node from the cache.

    You can also just use save events, but then you need to get the data from the ContentService and that data will come from the database. You can do that by doing Current.Services.ContentService. And it will be of type IContent instead of IPublishedContent.

  • Ayo Adesina 390 posts 967 karma points
    Jan 17, 2020 @ 16:41
    Ayo Adesina
    0

    OK that makes sense, I just tried to do this:

    CacheRefresherBase<ContentCacheRefresher>.CacheUpdated += ContentService_Saved; 
    

    but its not compling

    No overload for 'ContentService_Saved' matches delegate 'TypedEventHandler

    So I'm guessing there is a little more to it, can I have a code sample please?

  • Malthe Petersen 24 posts 158 karma points c-trib
    Jan 17, 2020 @ 16:46
    Malthe Petersen
    100

    Sorry I was on my IPad. Here is an example. It is because the arguments is different from the ones you have in your saved method:

       public void Initialize()
        {
            CacheRefresherBase<ContentCacheRefresher>.CacheUpdated += CacheUpdated;
        }
    
        private void CacheUpdated(ContentCacheRefresher sender, CacheRefresherEventArgs e)
        {
                var payloads = e.MessageObject as ContentCacheRefresher.JsonPayload[];
                if (payloads == null)
                {
                    return;
                }
           ...
           ...
        }
    

    The payload needs to be casted to the correct type. :-)

  • Malthe Petersen 24 posts 158 karma points c-trib
    Jan 21, 2020 @ 16:20
    Malthe Petersen
    1

    Did you get it to work? :-)

  • Ayo Adesina 390 posts 967 karma points
    Jan 21, 2020 @ 19:51
    Ayo Adesina
    0

    yes :-) Thank you!

  • Nik 1200 posts 4996 karma points MVP 2x c-trib
    Jan 21, 2020 @ 18:52
    Nik
    1

    Hi Ayo,

    A bit more of an explanation as to why you aren't getting results from the cache.

    When you save a content node it doesn't update the cache, instead it updates the underlying database which is accessible via the content service. The Umbraco cache only contains published content, so unless the content was also published (in which case you will want to subscribe to the Published event) you'll either get nothing, or you'll get outdated content.

    The published content is stored in the Umbraco Cache to make the frontend faster.

    Another thing to be aware of, this isn't the best way to access the Umbraco Cache when you in an event like this. This is because it is possible that an Umbraco Context is missing, instead I'd recommend taking advantage of the built in dependency inject and having a constructor paramter on your Component of type IUmbracoContextFactory. This will have a method called "EnsureUmbracoContext" which returns a UmbracoContextReference on which you can call UmbracoContext and this has a get method to get you IPublishedContent based on an ID.

    Hope that makes a bit of sense and explains why it's not working the way you are expecting.

    Nik

Please Sign in or register to post replies

Write your reply to:

Draft