Send for approval

Topics: General
Oct 25, 2011 at 12:55 PM

Hi All,

Should the "Send for approval" menu item send an e-mail?

If so how can I control which user(s) receive the e-mail?

Thanks,

Rob

Oct 25, 2011 at 1:53 PM
Edited Oct 25, 2011 at 1:54 PM

The way it is implemented now, it does not send any emails. In this discussion you can find the code that handles the event, and gets the list of uses that have "approve" right:

http://compositec1.codeplex.com/discussions/267415

 

Here's also the code I used recently, e-mail is hardcoded here, but if you combine it with the code in the link above, it should do the work:

 

using System;
using System.Net.Mail;
using System.Web;
using Composite.Core;
using Composite.Core.Application;
using Composite.Data;
using Composite.Data.ProcessControlled.ProcessControllers.GenericPublishProcessController;
using Composite.Data.Types;

namespace App_Code.Notifications
{
    /// <summary>
    /// Class that sends "awaiting approval" notification emails 
    /// </summary>
    [ApplicationStartup]
    public class Approval
    {
        private static readonly string EmailPageAvaitingApproval_To = "XXXXX@XXXXXXXXX.net";
        private static readonly string EmailPageAvaitingApproval_From = "donotreply@XXXXXXXXX.net";
        private static readonly string EmailPageAvaitingApproval_Subject = "Page awaiting approval: '%title%'";
        private static readonly string EmailPageAvaitingApproval_Body =
@"Hi XXXXXXX<br/>
<br/>
There's a new page that needs your approval<br/>
<br/>
<a href=""%url%"">
%title_htmlencoded%
</a>
<br/>
<br/>
Best regards<br/>
Notification robot @ www.XXXXXXXXXX.net";


        public static void OnBeforeInitialize() { }

        public static void OnInitialized()
        {
            DataEventSystemFacade.SubscribeToDataAfterUpdate<IPage>(MyPageAfterUpdate, true);
        }

        private static void MyPageAfterUpdate(object sender, DataEventArgs dataEventArgs)
        {
            IPage page = (IPage)dataEventArgs.Data;

            if (page.PublicationStatus != GenericPublishProcessController.AwaitingApproval)
            {
                return;
            }

            string pageUrl;

            try
            {
                using (var conn = new DataConnection(PublicationScope.Unpublished))
                {
                    pageUrl = string.Format("http://{0}{1}", HttpContext.Current.Request.Url.Host, conn.SitemapNavigator.GetPageNodeById(page.Id).Url);
                }
            }
            catch (Exception ex)
            {
                Log.LogError("Approval", "Failed to get page url");
                Log.LogError("Approval", ex);
                pageUrl = string.Empty;
            }


            Func<string, string> formatEmail =
                text => text
                .Replace("%title%", page.Title)
                .Replace("%title_htmlencoded%", HttpContext.Current.Server.HtmlEncode(page.Title))
                .Replace("%url%", pageUrl)
                .Replace("%url_attributeencoded%", HttpContext.Current.Server.UrlEncode(pageUrl)); ;

            MailMessage mail = new MailMessage(
                EmailPageAvaitingApproval_From,
                EmailPageAvaitingApproval_To,
                formatEmail(EmailPageAvaitingApproval_Subject),
                formatEmail(EmailPageAvaitingApproval_Body))
            {
                IsBodyHtml = true
            };

            SmtpClient smtp = new SmtpClient();
            smtp.Send(mail);
        }
    }
}
Oct 26, 2011 at 8:24 AM

Thanks, looks like that should do it.

Forgive me but I think I'm jumping in at the deep end here! Where should I put this code?

Oct 26, 2011 at 9:00 AM

>>  Where should I put this code?

I forgot to mention - create a *.cs file in "/App_Code" folder, or in a dll inside "/bin" folder. It will be executed since it has [Composite.Core.Application.ApplicationStartup] attribute on it

Jan 16, 2012 at 10:38 AM

I created a package for this functionality. It is better than suggested previously code since it a) does not send additional notification when a page is already in "waiting for approval" state b) Has an easier to edit email template

 

Way to setup:

1)      Download and install package http://docs.composite.net/Downloads/Codeplex/napernik/Notification.AutoApproval.zip

2)      Edit /App_Data/EmailTemplates/Notifications.PageAwaitingApproval.xml and specify email’s configuration – email’s content, recipient & sender e-mails.

 Default content looks like:

<EmailTemplate>
	<!-- Developer note:
	
	    	 Emails are comma separated, 
		 It is possible to reference settings from AppSettings by writing markup like {AppSetting:404Email.To} 
		 String parameters defined as {ParameterName}
		 Markup parameters defined as <render parameter="XmlParameterName" />
	 -->
	
	<from>no-reply@website.com</from>
	<to>recipient@website.com</to>
	<!--cc>a@website.com, b@website.com</cc-->
	<!--bcc>c@website.com, d@website.com/bcc-->
	<subject>Page awaiting approval: '{Title}'</subject>
	<html>
		<head></head>
		<body>
			There's a new page that needs your approval<br/>
			<br/>
			<a href="{Url}">
				{Title}
			</a>
			<br/>

			<br/>
			Best regards<br/>
			Notification service
		</body>
	</html>
</EmailTemplate>

 

If they want to send email to everybody who has right to approve, they should use code sample from this thread 

http://compositec1.codeplex.com/discussions/267415#post651254