How do I email the original submitter using Workflow in Sitecore?
Asked Answered
I

1

4

When an item is rejected by a reviewer in the workflow process, how do I inform the submitter? This seems like a very common situation, but I just see the most basic fields in the "Email Action" item:

To, From, Subject, Message

Is there a system variable for the user and then something for the user's email address? I'd expect it to be something like: $user.email.

Interact answered 19/5, 2011 at 18:57 Comment(1)
Hi I updated my answer with a code example. Hope it helps.Leet
L
5

Get the Mail Workflow Action from the shared source workflow actions.

Then you need to extend Populate context a little bit to make access to the last users easier. Here is my implementation on one of our recent projects:

    protected virtual void PopulateContext(WorkflowPipelineArgs args)
    {
        VelocityContext.Put("args", args);
        var item = args.DataItem;
        VelocityContext.Put("item", item);
        VelocityContext.Put("language", item.Language.CultureInfo.EnglishName);
        VelocityContext.Put("version", item.Version.Number);
        VelocityContext.Put("comment", args.Comments);
        VelocityContext.Put("processor", args.ProcessorItem);
        VelocityContext.Put("user", Context.User);
        Database masterDatabase = Factory.GetDatabase(DatabaseNames.Master);
        var workflow = masterDatabase.WorkflowProvider.GetWorkflow(item);
        var history = workflow.GetHistory(item);
        VelocityContext.Put("history", history);
        if (history.Length > 0)
        {
            string lastUser = history[history.Length - 1].User;
            MembershipUser membership = Membership.GetUser(lastUser);
            VelocityContext.Put("authorEmail",
                                membership != null
                                    ? membership.Email
                                    : DataAccessSettings.Workflow.WebQueryEmail);
        }
        VelocityContext.Put("state", workflow.GetState(item));

        var nextStateItem = GetNextState(args);
        VelocityContext.Put("nextState", nextStateItem != null ? nextStateItem.Name : string.Empty);
        VelocityContext.Put("time", DateTime.Now);
        VelocityContext.Put("previewUrl", string.Format("http://{0}/?sc_itemid=%7b{1}%7d&sc_mode=preview&sc_lang=en", DataAccessSettings.Site.HostName, item.ID.Guid));
        VelocityContext.Put("contentEditorUrl", string.Format("http://{0}/sitecore/shell/Applications/Content%20editor.aspx?fo=%7b{1}%7d&id=%7b{1}%7d&la=en&v=1&sc_bw=1", DataAccessSettings.Site.HostName, item.ID.Guid));
    }

    /// <summary>
    /// Processes the template, expanding all known values
    /// </summary>
    /// <param name="value">Template to process</param>
    /// <returns>Rendered template</returns>
    protected virtual string ProcessValue(string value, Item item)
    {
        var result = new StringWriter();
        try
        {
            Velocity.Evaluate(VelocityContext, result, "Extended mail action", value);
        }
        catch (ParseErrorException ex)
        {
            Log.Error(string.Format("Error parsing template for the {0} item \n {1}",
                                    item.Paths.Path, ex), this);
        }
        return result.GetStringBuilder().ToString();
    }

    #region helpers

    private static Item GetNextState(WorkflowPipelineArgs args)
    {
        Item command = args.ProcessorItem.InnerItem.Parent;
        string nextStateID = command["Next State"];
        if (nextStateID.Length == 0)
        {
            return null;
        }

        return args.DataItem.Database.Items[ID.Parse(nextStateID)];
    }

    private string ProcessFieldValue(string fieldName, Item item)
    {
        string value = item[fieldName];
        if (value.Length > 0)
        {
            return ProcessValue(value, item);
        }
        return value;
    }

    #endregion

You can use $authoremail when setting up the email template.

Hope that helps.

Leet answered 19/5, 2011 at 20:58 Comment(1)
This looks really helpful. Thanks! This just seems like such a basic feature that I don't feel like I should need to use so much code.Interact

© 2022 - 2024 — McMap. All rights reserved.