KB's

How to notify roles that a page has been sent through workflow

  • Introduction



    Programmatically, Sitefinity can send users in a role e-mail notifications that a page has gone through workflow. This includes pages saved, sent for approval, approved, declined or published. To set up this feature, you must create two custom classes. One will inherit the Activity class and the other will inherit the PageWorkflow class.

    Instructions



    The code sample below shall show you how to notify a role that a page has been sent for approval. To activate this notification system, follow these instructions:

    1.    Create a class file called CustomPageWorkflow.cs in your App_Code folder
    2.    Remove the code in that file and add this code
  •   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Workflow;
    using Telerik.Cms;
    using System.Net.Mail;
      
    /// <summary>
    /// Summary description for PageNotifications
    /// </summary>
    /// <summary>  
    /// Custom Activity that sends emails out.  
    /// </summary>  
    public class EmailActivity : Activity
    {
          
        public EmailActivity() { Status = ApprovalStatus.ForApproval; }
        public ApprovalStatus Status;
        private Guid workflowID;
      
      
        //Every workflow request has an ID. Get that ID.
        public Guid WorkflowID
        {
            get
            {
                object value = base.GetValue("WorkflowID");
                return value != null ? (Guid)value : Guid.Empty;
            }
            set
            {
                base.SetValue("WorkflowID", value);
            }
        }
      
      
        public override ActivityExecutionStatus Execute(WorkflowRuntime runtime)
        {
            //Get the workflow ID, so you can get the row in the database for it
            CmsManager manager = new CmsManager();
            ICmsPage page = manager.Provider.GetPageByWorkflow(this.WorkflowID);
      
            //send an email  
            MailMessage mailMsg = new MailMessage();
      
            // Subject and Body     
            switch (Status)
            {
                //Send a message based on status. If the page is approved, then send a message that is has been approved
                case ApprovalStatus.Approved:
                    mailMsg.Subject = "page approved";
                    mailMsg.Body += String.Format("The '{0}' page has been approved. To approve the page, please go to URL: http://{1}/Sitefinity/Admin/Pages.aspx", page.Name, System.Web.HttpContext.Current.Request.Url.Host);
                    break;
                case ApprovalStatus.Archived:
                    break;
                //If declined, then send a message that it has been declined
                case ApprovalStatus.Declined:
                    mailMsg.Subject = "page declined";
                    mailMsg.Body += String.Format("The '{0}' page has been declined."); 
                    break;
                case ApprovalStatus.Draft:
                    break;
                //If it is for approval, then send a message saying it has been for approval
                case ApprovalStatus.ForApproval:
                    mailMsg.Subject = "page pending approval";
                    mailMsg.Body += String.Format("The '{0}' page has been sent for approval. To approve the page, please go to URL: http://{1}/Sitefinity/Admin/Pages.aspx", page.Name, System.Web.HttpContext.Current.Request.Url.Host);
                    break;
                case ApprovalStatus.None:
                    break;
                case ApprovalStatus.Published:
                    mailMsg.Subject = "page pending approval";
                    mailMsg.Body += String.Format("The '{0}' page has been published. To view the page, please go to this URL: http://{1}/{2}.aspx", page.Name, System.Web.HttpContext.Current.Request.Url.Host, page.Name);
                    break;
                default:
                    break;
            }
            if (!mailMsg.Subject.Equals(string.Empty))
            {
                // To    
                string approversRole = "Content Contributor";
                string[] usernames = Telerik.Security.UserManager.Default.GetUsersInRole(approversRole);
                //For every user, send him or her an e-mail
                foreach (string username in usernames)
                {
                    System.Web.Security.MembershipUser user = Telerik.Security.UserManager.Default.GetUser(username);
                    mailMsg.To.Add(user.Email);
                }
      
                // Init SmtpClient and send     
                SmtpClient smtpClient = new SmtpClient();
                if (mailMsg.To.Count <= 0)
                    mailMsg.To.Add(mailMsg.From);
      
                smtpClient.Send(mailMsg);
            }
      
            return base.Execute(runtime);
        }
        /// <summary> 
        /// Summary description for CustomPageWorkflow 
        /// </summary> 
        /// 
          
    }
     
     
    public class CustomPageWorkflow : PageWorkflow
       {
           protected override void LoadActivities()
           {
      
               // Step 1: wait to send for approval
               //****************************************************************
               ListenActivity listen = Activity.Load<ListenActivity>(base.Transaction);
               base.AddActivity(listen);
      
               EventActivity eventActivity = Activity.Load<EventActivity>(base.Transaction);
               eventActivity.Caption = "SendForApproval";
               eventActivity.CommandName = "SendForApproval";
               listen.AddActivity(eventActivity);
      
               SetApprovalStatusActivity setStatus = Activity.Load<SetApprovalStatusActivity>(base.Transaction);
               setStatus.ApprovalStatus = ApprovalStatus.ForApproval;
               eventActivity.AddActivity(setStatus);
      
               CallExternalMethodActivity callMethod = Activity.Load<CallExternalMethodActivity>(base.Transaction);
               callMethod.InterfaceType = typeof(CmsWorkflowService);
               callMethod.MethodName = "UpdateStatus";
               callMethod.Parameters = new object[] { base.ItemId, setStatus.ApprovalStatus };
               eventActivity.AddActivity(callMethod);
      
               // Step 2: wait for denial or approval
               //******************************************************************
               listen = Activity.Load<ListenActivity>(base.Transaction);
               base.AddActivity(listen);
      
               eventActivity = Activity.Load<EventActivity>(base.Transaction);
               eventActivity.Caption = "Approve";
               eventActivity.CommandName = "Approve";
               listen.AddActivity(eventActivity);
      
               setStatus = Activity.Load<SetApprovalStatusActivity>(base.Transaction);
               setStatus.ApprovalStatus = ApprovalStatus.Approved;
               eventActivity.AddActivity(setStatus);
      
               callMethod = Activity.Load<CallExternalMethodActivity>(base.Transaction);
               callMethod.InterfaceType = typeof(CmsWorkflowService);
               callMethod.MethodName = "UpdateStatus";
               callMethod.Parameters = new object[] { base.ItemId, setStatus.ApprovalStatus };
               eventActivity.AddActivity(callMethod);
      
      
               eventActivity = Activity.Load<EventActivity>(base.Transaction);
               eventActivity.Caption = "Decline";
               eventActivity.CommandName = "Decline";
               listen.AddActivity(eventActivity);
      
               setStatus = Activity.Load<SetApprovalStatusActivity>(base.Transaction);
               setStatus.ApprovalStatus = ApprovalStatus.Declined;
               eventActivity.AddActivity(setStatus);
      
               callMethod = Activity.Load<CallExternalMethodActivity>(base.Transaction);
               callMethod.InterfaceType = typeof(CmsWorkflowService);
               callMethod.MethodName = "UpdateStatus";
               callMethod.Parameters = new object[] { base.ItemId, setStatus.ApprovalStatus };
               eventActivity.AddActivity(callMethod);
      
      
               // Step 3: check condition
               //*********************************************************************
      
               IfElseActivity ifElse = Activity.Load<IfElseActivity>(base.Transaction);
               ifElse.InterfaceType = typeof(CmsWorkflowService);
               ifElse.MethodName = "IsApproved";
               ifElse.Parameters = new object[] { base.ItemId };
               base.AddActivity(ifElse);
      
               ConditionActivity trueActivity = Activity.Load<ConditionActivity>(base.Transaction);
               trueActivity.Condition = true;
               ifElse.AddActivity(trueActivity);
      
               ConditionActivity falseActivity = Activity.Load<ConditionActivity>(base.Transaction);
               falseActivity.Condition = false;
               ifElse.AddActivity(falseActivity);
      
               // Step 4: wait to publish
               //*********************************************************************
               listen = Activity.Load<ListenActivity>(base.Transaction);
               trueActivity.AddActivity(listen);
      
               eventActivity = Activity.Load<EventActivity>(base.Transaction);
               eventActivity.Caption = "Publish";
               eventActivity.CommandName = "Publish";
               listen.AddActivity(eventActivity);
      
               callMethod = Activity.Load<CallExternalMethodActivity>(base.Transaction);
               callMethod.InterfaceType = typeof(CmsWorkflowService);
               callMethod.MethodName = "PublishPage";
               callMethod.Parameters = new object[] { base.ItemId };
               eventActivity.AddActivity(callMethod);
      
               setStatus = Activity.Load<SetApprovalStatusActivity>(base.Transaction);
               setStatus.ApprovalStatus = ApprovalStatus.Published;
               eventActivity.AddActivity(setStatus);
           }
       }

     

    3.    Open the Web.config of your application
    4.    Add the CustomWorkflow attribute to signify your custom class as shown below:

    <add connectionStringName="DefaultConnection" pageWorkflowType="CustomPageWorkflow" allowPageHistory="true" 
              allowPageWorkflow="true" cachingProviderName="ASPNET" name="Sitefinity" 
              type="Telerik.Cms.Data.DefaultProvider, Telerik.Cms.Data" /> 
     

    Customizations



    You might want to notify users that a page has been published or declined. To do so, follow the steps above and then follow these instructions:

    1.    Locate this line of code:

    public EmailActivity() { Status = ApprovalStatus.ForApproval; } 


    2.    Delete the .ForApproval
    3.    Enter in a “.” and then select the workflow step you’d like to use as shown below:

    To notify users when a page is published, the code would look like this:

        public EmailActivity() { Status = ApprovalStatus.Published;} 

    To change the message that is sent when a workflow step has been completed, follow these steps:

    1.    Locate the workflow section message that you’d like to change. Here, we’ll use the publish section:

    case ApprovalStatus.Published: 
                    mailMsg.Subject = "page pending approval"
                    mailMsg.Body += String.Format("The '{0}' page has been published. To view the page, please go to this URL: http://{1}/{2}.aspx", page.Name, System.Web.HttpContext.Current.Request.Url.Host, page.Name); 
                    break
     

    2.    Change the mailMsg.Body property to the text you’d like to send.

    To change the role that receives the e-mail, follow these steps:

    1.    Locate this line of code:

    string approversRole = "Content Contributor"


    2.    Change the property to the role that shall receive the notifications