Copied to clipboard

Flag this post as spam?

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


  • Marcelo S. 12 posts 57 karma points
    May 07, 2015 @ 15:40
    Marcelo S.
    0

    Doubt about Umbraco Published Event

    Hi guys,

    I'm still "fighting" with Umbraco here and now I got a doubt that I'm not able to find out by myself.

    I'm using the version: 7.2.4

     

    ---- My goals:

    1 - Each new Content of the type "Noticia" published by the user, I need to send an email for ALL active Members about this new content.

    2 - This mail has to have on its body, the URL to the Content (besides other properties that I already know how to bring).

     

    ---- The way I think I could reach my goals:

    1 - Creating a new class inheriting from IApplicationEventHandler and then use the Event Published to check the Alias content and send the emails.

     

     --- What I have coded so far:

    public class ApplicationEventHandler : IApplicationEventHandler
        {
            public ApplicationEventHandler()
            {
                //Umbraco.Core.Publishing.PublishingStrategy.Published += Published;
                ContentService.Published += Published;
            }
    
            private static void Published(IPublishingStrategy sender, PublishEventArgs<IContent> args)
            {
                //Get all members approved and active
                IEnumerable<IMember> membros = UmbracoContext.Current.Application.Services.MemberService.GetAllMembers().Where(w => w.IsApproved && !w.IsLockedOut);
                var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    
                // Rule to send email to the members.
                foreach (var conteudo in args.PublishedEntities)
                {
                    if (conteudo.ContentType.Alias == "Noticia" &&
                        conteudo.CreateDate == conteudo.UpdateDate)
                    {
                        try
                        {
                            string url = umbracoHelper.Url(conteudo.Id, Umbraco.Web.Routing.UrlProviderMode.Absolute);
                            string resumoNoticia = umbracoHelper.Truncate(conteudo.Properties["descricao"].Value.ToString(), 200, true).ToHtmlString();
                            string titulo = "SAC - Nova Notícia";
                            string corpo = string.Format("Uma nova notícia foi adicionada, o titulo é: {0}, um resumo é: {1}, e a URL para acesso é: {2}", conteudo.Name, resumoNoticia, url);
                            string paraList = string.Join(",", membros.Select(s => s.Email));
    
                            using (SmtpClient smtp = new SmtpClient())
                            {
                                var de = new MailAddress(((NetworkCredential)smtp.Credentials).UserName, "SAC Automático");
                                using (var message = new MailMessage())
                                {
                                    message.Body = corpo;
                                    message.Subject = titulo;
                                    message.From = de;
                                    message.Bcc.Add(paraList);
                                    message.IsBodyHtml = true;
    
                                    smtp.Send(message);
                                }
                            }
                        }
                        catch (Exception) { }
                    }
                }
            }
        }

     

    --- The issues:

    1 - The code "umbracoHelper.Url(conteudo.Id, Umbraco.Web.Routing.UrlProviderMode.Absolute)" returns to me "#" not the URL absolute, what am I doing wrong?

    2 - This test part "conteudo.CreateDate == conteudo.UpdateDate". I'm using it to check if is the first Publish from the content, because I don't want to bother my members, sending email to them for each content change. But this approach is not working, because if the user only Saves the content for Publish later, my logic breaks. Is there another way to reach my goals? Basically I need to know the first time a content is being published. 

    Another approach that I thought, would be, after sending the email I could update some property inside the content, I'm only afraid to create a recursive call to the Published event.

     

    Thank you for any help or thoughts, and sorry about my English.

Please Sign in or register to post replies

Write your reply to:

Draft