Copied to clipboard

Flag this post as spam?

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


  • Trevor Husseini 20 posts 151 karma points
    Feb 06, 2021 @ 01:26
    Trevor Husseini
    0

    Can you detect a "Save and publish" event in a DelegatingHandler?

    Aloha Again,

    I have a class that inherits from DelegatingHandler. It implements the SendAsync method whose logic is as follows:

    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        if (request.RequestUri.AbsolutePath.ToLower() == "/umbraco/backoffice/umbracoapi/content/postsave")
        {
            return base.SendAsync(request, cancellationToken)
                .ContinueWith(task =>
                {
                    var response = task.Result;
    
                    try
                    {
                        var data = response.Content;
                        var content = ((ObjectContent)(data)).Value as ContentItemDisplay;
    
                        if (content != null && content.ContentTypeAlias == "myCustomDocType")
                        {
                            //Do something
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.Error<WebApiHandler>("Error adding custom publishing message.", ex);
                    }
    
                    return response;
                });
        }
    
        return base.SendAsync(request, cancellationToken);
    }
    

    Is there a way to differentiate between the node being saved and the node being saved and published?

    From what I've seen, PostSave is called regardless of the user's action (save or save and publish) and there aren't any extra requests made strictly in the case of a save and publish. That rules out the ability to "listen in" on a different URL. After comparing the task's result between cases, I haven't spotted any key differences between the 2 responses. I've read elsewhere of a HasIdentity property on an entity and even if that were available to me, it would only solve half of my problem as I'm trying to detect when a new or existing node is saved and published.

  • Mario Lopez 168 posts 952 karma points MVP 3x c-trib
    Feb 06, 2021 @ 06:10
    Mario Lopez
    0

    What's the reason for using DelegatingHandler and not the Umbraco services' events? What are you trying to achieve?

  • Trevor Husseini 20 posts 151 karma points
    Feb 07, 2021 @ 22:57
    Trevor Husseini
    0

    Mario,

    Thanks for the reply!

    I'm using a delegating handler because this piece of code was originally written on v7 and was then ported to a v8 installation. I'm trying to exhaust all possible options of reworking this code before I recommend refactoring. From what I gathered, I can probably move this code into a Composer and leverage the Notifications Service via DI.

    The intent of this code is to notify a user of the result of an action in the CMS via the Backoffice notification ribbons.

  • Trevor Husseini 20 posts 151 karma points
    Feb 10, 2021 @ 01:02
    Trevor Husseini
    100

    The solution was to check the Notifications property of the content object for any pre-existing messages. A strict save entails a Notification whose Header is "Content saved". A save and publish means the Notification's Header is "Content published".

    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        if (request.RequestUri.AbsolutePath.ToLower() == "/umbraco/backoffice/umbracoapi/content/postsave")
        {
            return base.SendAsync(request, cancellationToken)
                .ContinueWith(task =>
                {
                    var response = task.Result;
    
                    try
                    {
                        var data = response.Content;
                        var content = ((ObjectContent)(data)).Value as ContentItemDisplay;
                        var publicationMessage = content.Notifications.FirstOrDefault(x => x.Header.ToLower() == "content published");
    
                        if (content != null && content.ContentTypeAlias == "myCustomDocType" && publicationMessage != null)
                        {
                            //Do something
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.Error<WebApiHandler>("Error adding custom publishing message.", ex);
                    }
    
                    return response;
                });
        }
    
        return base.SendAsync(request, cancellationToken);
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft