| 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 = "Send for approval"; |
| eventActivity.CommandName = "SendForApproval"; |
| listen.AddActivity(eventActivity); |
| |
| SetApprovalStatusActivity setStatus = Activity.Load<SetApprovalStatusActivity>(base.Transaction); |
| setStatus.ApprovalStatus = ApprovalStatus.ForApproval; |
| eventActivity.AddActivity(setStatus); |
| |
| //we want an email to go to the approvers when a page is sent for approval |
| //add the eMail activity to the event |
| EmailActivity emailActivity = Activity.Load<EmailActivity>(base.Transaction); |
| emailActivity.WorkflowID = base.ItemId; |
| emailActivity.Status = ApprovalStatus.ForApproval; |
| |
| eventActivity.AddActivity(emailActivity); |
| |
| // 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); |
| |
| 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); |
| |
| |
| |
| // 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); |
| |
| CallExternalMethodActivity 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); |
| } |
| } |
| |