Workflow RSS Feed Custom Email Action

I have worked with a number of clients that have asked why the email notifications they receive from workflow didn’t reflect what is available from the RSS feed within the workbox.

This seems to have been a goal of a number of developers in the past, but nothing concrete has been created to replicate the feature. There is a awesome workaround from Nick Wesselman using Rss2email. http://www.techphoria414.com/Blog/2012/April/Sitecore_Workflow_Emails_with_rss2email

As part of a recent hacking session I decided to see if I could replicate the RSS feed within a more advance email action.

Solution
The solution is available as a Sitecore package on the marketplace or the source is available of GitHub at the bottom of this blog. Please feel free to extend and contribute.

Workflow history

Using the GetWorkflow().GetHistory(Item) API gives full access to the entire workflow history or a particular Sitecore Item.

   var workflowItemHistory = Item.State.GetWorkflow().GetHistory(Item);

However on testing this code threw up an unexpected error. Due to the email action being called from a command the workflow command hasn’t been fully executed, so the workflow history doesn’t contain the currently executing workflow command. This was solved by creating a function that gets the current workflow command that has fired, working out the target state of the action before appending this data to a collection of workflow history.

        private ItemWorkflowHistory GetCurrentWorkflowHistoryItem()
        {

            var history = new ItemWorkflowHistory()
            {
                ItemDateTime = DateTime.Now,
                User = Sitecore.Context.GetUserName(),
                PreviousState = workflowPipelineArgs.DataItem.State.GetWorkflowState().DisplayName,
                CurrentState = GetCorrectWorkflowState().DisplayName,
                Comment = workflowPipelineArgs.Comments
            };

            return history;
        }

private WorkflowState GetCorrectWorkflowState()
        {
            var emailAction = GetEmailAction();

            var command = emailAction.Parent;

            var nextStateId = command["Next state"];

            var itemWorkflow = Sitecore.Data.Database.GetDatabase("master").WorkflowProvider.GetWorkflow(workflowPipelineArgs.DataItem);

            return itemWorkflow.GetState(nextStateId.ToString());
        }

Workflow commands
The biggest challenge was to build the command links to allow the user to fire the correct commands at the relevant stage of workflow.

This suffered from the same issue as the workflow history, the correct workflow commands weren’t shown because the currently executing command hasn’t completed. Again this was solved using the method above of getting the firing command and getting its target state of workflow. The correct workflow commands can then be taken from this target state of workflow.

private string GetCommandLinks(Item workflowItem, WorkflowState state)
        {
            var sb = new StringBuilder();

            var workflow = Sitecore.Data.Database.GetDatabase("master").WorkflowProvider.GetWorkflow(workflowItem);

            var commands = workflow.GetCommands(state.StateID);

            sb.Append("<ul>");

            foreach (var command in commands)
            {
                var submit = Tools.GetContentEditorLink(ContentEditorMode.Submit, workflowItem, HostName, new ID(command.CommandID));
                var submitComment = Tools.GetContentEditorLink(ContentEditorMode.Submit, workflowItem,HostName, new ID(command.CommandID));

                sb.Append("<li><a>" + command.DisplayName + "</a> or <a>" + command.DisplayName + " & comment</a></li>");
            }

            string editLink = Tools.GetContentEditorLink(ContentEditorMode.Editor, workflowItem, HostName, ID.NewID);
            string previewLink = Tools.GetContentEditorLink(ContentEditorMode.Preview, workflowItem,HostName, ID.NewID);

            sb.Append("<li><a>Edit</li>");
            sb.Append("<li><a>Preview</li>");
            sb.Append("<li><a />Workbox</li>");

            sb.Append("</ul>");

            return sb.ToString();
        }

Reviewing the command links in the Workbox RSS feed I was able to build a helper class to build the command links in the correct URL format for the links.

public static string GetContentEditorLink(ContentEditorMode contentEditorMode, Item item, string hostName, ID commandId)
        {
            switch (contentEditorMode)
            {
                case ContentEditorMode.Editor:
                    return ("http://" + hostName.Replace("http://", "") + "/sitecore/shell/Applications/Content%20editor.aspx?fo=" + item.ID.ToString() + "&id=" + item.ID.ToString() + "&la=" + item.Language.Name + "&v=" + item.Version.Number.ToString() + "&sc_bw=1");

                case ContentEditorMode.Preview:
                    return ("http://" + hostName.Replace("http://", "") + "/sitecore/shell/feeds/action.aspx?c=Preview&id=" + item.ID.ToString() + "&la=" + item.Language.Name + "&v=" + item.Version.Number.ToString());

                case ContentEditorMode.Submit:
                    return ("http://" + hostName.Replace("http://", "") + "/sitecore/shell/feeds/action.aspx?c=Workflow&id=" + item.ID.ToString() + "&la=" + item.Language.Name + "&v=" + item.Version.Number.ToString() + "&cmd=" + commandId.ToString());

                case ContentEditorMode.SubmitComment:
                    return ("http://" + hostName.Replace("http://","") + "/sitecore/shell/feeds/action.aspx?c=Workflow&id=" + item.ID.ToString() + "&la=" + item.Language.Name + "&v=" + item.Version.Number.ToString() + "&cmd=" + commandId.ToString() + "&nc=1");
            }
            return "";
        }
    }

Workflow History

20140521-194216-70936959.jpg

20140521-194216-70936793.jpg

Source
https://github.com/komainu85/AdvancedEmailAction

Sitecore Marketplace
https://marketplace.sitecore.net/en/Modules/Sitecore_Advanced_Email_Action.aspx