How do I email the original submitter using Workfl

2020-04-08 14:47发布

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.

1条回答
家丑人穷心不美
2楼-- · 2020-04-08 15:07

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.

查看更多
登录 后发表回答