Sitefinity ASP.NET CMS - Content Management System

KB Article

Home >  Support >  Knowledge Base >  KB Article
How to notify approvers when a News item is sent for approval - ID#981
Rating:
Last Modified: 7/10/2008
Related categories: Workflow;

Article information

Article relates to

 Sitefinity 3.x

Created by

Vlad


Since the workflow event notification is missing in Sitefinity as built-in functionality, here is how it could be implemented manually. The example below demonstrates how to send emails to the users belonging to Approvers role when a News item has been sent for approval. The same solution could be implemented for other modules based on Generic Content and any of the workflow events could be handled.

EXAMPLE:

1.  
Create a code-behind class for ~/Sitefinity/Admin/ControlTemplates/News/ControlPanelEdit.ascx (~/Sitefinity/Admin/ControlTemplates/News/ControlPanelEdit.ascx.cs):

using System; 
using System.Web.UI.WebControls; 
using Telerik.Workflow; 
using System.Collections.Generic; 
using Telerik.Workflow.WebControls; 
using Telerik.Cms.Engine; 
using Telerik.News; 
using Telerik.News.WebControls.Admin; 
using System.Net.Mail; 
 
public partial class Sitefinity_Admin_ControlTemplates_News_ControlPanelEdit : System.Web.UI.UserControl 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        workflowMenu.Command += new CommandEventHandler(workflowMenu_Command); 
    } 
 
    void workflowMenu_Command(object sender, CommandEventArgs e) 
    { 
        WorkflowInstance workflow = ((WorkflowMenu)sender).GetWorkflow(); 
        if (workflow != null
        { 
            List<EventActivity> commands = new List<EventActivity>(); 
            this.LoadCommands(commands, workflow.Activity.Activities); 
            int idx = int.Parse((string)e.CommandArgument); 
 
            // The workflow events you could handle are: SendForApproval, Approve, Decline, Publish 
            if ((commands.Count > idx) && (commands[idx].CommandName.Equals("SendForApproval"))) 
            { 
                // Getting currently modified content and format the message 
                string providerName = ((ControlPanel)this.Parent.Parent).ProviderName; 
                NewsManager manager = new NewsManager(providerName); 
                IContent content = manager.Content.GetCurrentState(this.contentView.ContentID); 
                string message = String.Format("'{0}' news item has been sent for approval\n\nhttp://{2}{3}/Sitefinity/Admin/Modules.aspx?module=News&Id={1}&provider={4}", content.GetMetaData("Title"), content.ID, Request.Url.Host, Request.ApplicationPath.Length == 1 ? null : Request.ApplicationPath, providerName); 
 
                MailMessage mailMsg = new MailMessage(); 
 
                // From  
                MailAddress mailAddress = new MailAddress("cms-approval@yourcompany.com"); 
                mailMsg.From = mailAddress; 
 
                // Subject and Body  
                mailMsg.Subject = "News item pending approval"
                mailMsg.Body = message; 
 
                // To 
                string approversRole = "Approver"
                string[] usernames = Telerik.Security.UserManager.Default.GetUsersInRole(approversRole); 
                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(); 
                smtpClient.Send(mailMsg); 
            } 
        } 
    } 
 
    private void LoadCommands(List<EventActivity> commands, IList<Activity> activities) 
    { 
        foreach (Activity act in activities) 
        { 
            if (act is EventActivity) 
                commands.Add((EventActivity)act); 
            this.LoadCommands(commands, act.Activities); 
        } 
    } 
 

2. Declare
the code-behind file in the template .ASCX file (~/Sitefinity/Admin/ControlTemplates/News/ControlPanelEdit.ascx):

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ControlPanelEdit.ascx.cs" Inherits="Sitefinity_Admin_ControlTemplates_News_ControlPanelEdit" %> 
 

Please note that the attached template file is from Sitefinity 3.2 SP1.

Article Files

  • NewsWorkflowNotification.zip



  • Article Comments

    Seth Cleaver, 6/20/2008
    Hi, This is great Can this be take further easily - On publish send a email template that is "Newsletters" module and send it to a subscriber list that is in the newletter module. eg. Say we have media releases news module and we have a subscribers form on the site that says "Like to be notified of latest media releases? Subscribe here." On publish of a news item it sends notification to these subscribers. Having the template kept in the newsletters module would allow users to change template if required. Sounds like a good idea? Thanks

    Telerik Admin, 7/1/2008
    Thanks for the great ideas. Actually we have such plans for the future and we will use exactly the Newsletters module for this purpose. But before adding this functionality, we will firstly integrate Windows Workflow Foundation (WWF) into Sitefinity, replacing the current implementation.

    Josh Morales, 10/22/2008
    how do you use this to enable page modification notifications?


    Please Sign In to rate this article or to add it to your favorites.