Issues with handling Save and Publish events in Umbraco - Need Help!
b
Hello everyone, I am encountering some challenges with handling Save and Publish events in Umbraco, specifically using SaveAndPublishHandler class. Here are the details of my issue: **Problem Description**: I have implemented a SaveAndPublishHandler class in Umbraco to handle Saved and Published events for content items. The goal is to send an HTTP POST request to an external endpoint whenever content is saved or published with a specific ParentId (in my case, ParentId 1249). *Code : *
Copy code
c#
        public SaveAndPublishHandler(IContentService contentService)
        {
            _contentService = contentService;
        }

        public void Initialize()
        {
            ContentService.Saved += ContentService_Saved;
        }

        public void Terminate()
        {
            ContentService.Saved -= ContentService_Saved;
        }

        private void ContentService_Saved(IContentService sender, SaveEventArgs<IContent> e)
        {
            foreach (var content in e.SavedEntities)
            {
                if (content.ParentId == 1249)
                {
                    var dataToSend = new { ContentId = content.Id, Title = content.Name };
                    var json = Newtonsoft.Json.JsonConvert.SerializeObject(dataToSend);
                    var contentToSend = new StringContent(json, Encoding.UTF8, "application/json");

                    using (var httpClient = new HttpClient())
                    {
                        var endpointUrl = "http://localhost:65137/umbraco/surface/UpdateApprovedAccount/UpdateApprovedAccountV2"; 

                        var response = httpClient.PostAsync(endpointUrl, contentToSend).Result;

                        var responseContent = response.Content.ReadAsStringAsync().Result;
                    }
                }
            }
        }
Composing :
Copy code
c#
public void Compose(Composition composition) {
composition.Components().Append<SaveAndPublishHandler>();
}
l
@baristaner what is going wrong with this? At a glance it looks like it should work?
Docs are here for this https://docs.umbraco.com/umbraco-cms/v/13.latest-lts/reference/notifications/notification-handler & https://docs.umbraco.com/umbraco-cms/v/13.latest-lts/reference/notifications/contentservice-notifications assuming your working with version 13? To set this up, i would expect the following:
Copy code
public class DontShoutComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        builder.AddNotificationHandler<ContentPublishingNotification, DontShout>();
    }
}
And the actual method that gets run
Copy code
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Notifications;

namespace Umbraco.Docs.Samples.Web.Notifications;

public class DontShout : INotificationHandler<ContentPublishingNotification>
{
    public void Handle(ContentPublishingNotification notification)
    {
        foreach (var node in notification.PublishedEntities)
        {
            if (node.ContentType.Alias.Equals("announcement"))
            {
                var newsArticleTitle = node.GetValue<string>("title");
                if (!string.IsNullOrWhiteSpace(newsArticleTitle) && newsArticleTitle.Equals(newsArticleTitle.ToUpper()))
                {
                    notification.CancelOperation(new EventMessage("Corporate style guideline infringement",
                        "Don't put the announcement title in upper case, no need to shout!",
                        EventMessageType.Error));
                }
            }
        }
    }
}
Personally I would run the actual post of data inside a hangfire task, which means when a content editor publishes they dont have a big delay if the API call takes a few seconds, it also means if the API call fails, it will try multiple times.
b
Nope,working with 8.17 the problem is it saves the content but it does not send the post request
l
If you add a break point into the method, does it get hit? I assume not?
b
Makes sense
b
I’ll try it and let u know thanks
Seems like it does hit the breakpoint
But the post request does not work
l
The request isn’t being made by the browser so won’t show up in the network tab. You’re making a request to the Umbraco surface controller from code, I would take whatever logic is in that surface controller and run that in the handler
b
Yeah i completely forget about that Seems to be working thanks for your help.
241 Views