Copied to clipboard

Flag this post as spam?

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


  • blackhawk 313 posts 1368 karma points
    Aug 22, 2017 @ 21:29
    blackhawk
    0

    Learning more about Content Service Events

    I'm new to learning about Umbraco events. I've been reading a few sources online to get some footing in this area of Umbraco.

    Here is my code, which works in sending an email via SMTP, and when ContentService_Published fires.

    using System;
    using System.Net.Mail;
    using Umbraco.Core;
    using Umbraco.Core.Logging;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    using Umbraco.Core.Publishing;
    
    
    namespace My.Namespace
    {
        public class MyEventHandler : ApplicationEventHandler
        {
    
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Published += ContentServicePublished;
            }
    
            private void ContentServicePublished(IPublishingStrategy sender, PublishEventArgs<IContent> args)
            {
                foreach (var node in args.PublishedEntities)
                {
                    if (node.ContentType.Alias == "contentPage")
                    {
                        SendMail();
                        LogHelper.Info(typeof(LogWhenPublished), "Make note of this action within the Umbraco Log file, relating to my content type alias contentPage.");
                    }
                }
            }
            private void SendMail()
            {
                try
                {
                    MailMessage mailMessage = new MailMessage();
                    mailMessage.To.Add("[email protected]");
                    mailMessage.From = new MailAddress("[email protected]");
                    mailMessage.Subject = "MY Email Notification Subject";
                    mailMessage.Body = "Someone just sent a request for approval for the following content node item";
                    SmtpClient smtpClient = new SmtpClient();
                    smtpClient.Send(mailMessage);
                }
                catch (Exception ex)
                {
                    LogHelper.Info(typeof(LogWhenPublished), "This action failed, relating to my content type alias contentPage");
                }
            }
        }
    }
    

    I have two questions in regards to improving this code...

    1. How can I upgrade this so that it only fires at specific events, like...

      • Only when an editor saves the content item
      • Only when an editor sends for approval on an content item
    2. How can I include information about the content item in this class? for example I want to include the title of the content item in my email.

    Thanks for any tips on this!

  • John Bergman 483 posts 1132 karma points
    Aug 22, 2017 @ 22:43
    John Bergman
    1

    There are quite a few events you can connect to inside ContentService, this example just happens to use published.

    To include the title, you would need to pass node into SendMail(), and then set the title using node.

  • blackhawk 313 posts 1368 karma points
    Aug 23, 2017 @ 15:09
    blackhawk
    100

    Thanks! That is awesome info to know! I made progress today and was able to solve my quesitons by reviewing the various "signatures" based on the ContentService events, and place the desired one in the proper place within my custom .cs file. I also discovered the use of entities. I can get the title of a content node directly from this process.

Please Sign in or register to post replies

Write your reply to:

Draft