Sitecore’s workbox for workflow sucks

Not only does it suck, another “Advanced” workbox also sucks. Seriously. I had to restore the entire Sitecore databases (we have a staging, so FOUR databases) from the night before because someone accidentally approved all. And yeah, work was lost.

Who creates an “approve all” without a “Are you sure” popup????? ARGGGHHHHHHH

So I took the best of both and created a better, simple, ugly version that works nicely. PLUS, if there are multiple approvals, etc., I have a comment box that will be applied to all of them.

I’m not delving into the whole, create a package aspect of Sitecore, to be honest, I want it to be standalone, because I am totally weird like that.

This blog is for my bragging moments only, not for resale but if you want to use or adapt the code, feel free. It’s FrankenCode as it is, so complain not to me.

The entire process was helped using Jetbrains Resharper but if you can’t afford that, the free JetBrains DotPeek is fantastic!

Anyway, using that I created this nice boring workbox that is simple and does everything my Sitecore users need: approve, reject and cancel (draft form only).

private static MyWorkflowItems GetWorkflowItem(Item item, IWorkflow workflow, int itemCount, string relatedPath, string workFlowState)
     {

         MyWorkflowItems workflowItems = new MyWorkflowItems();

         workflowItems.SerialNumber = itemCount.ToString();
         workflowItems.Name = item.Name;
         workflowItems.Path = item.Paths.FullPath;
         DateTime dateTime = DateUtil.ParseDateTime(item.Fields["__Updated"].Value, DateTime.Today);
         workflowItems.ModifiedDate = dateTime.ToString("yyyy/MM/dd");
         workflowItems.Language = item.Language.ToString();
         WorkflowEvent[] workflowEvent = workflow.GetHistory(item);
         var newState = workFlowState;
         var oldState = "?";
         if (workflowEvent.Length > 0)
         {
             var mostRecentEvent = workflowEvent[workflowEvent.Length - 1];
             oldState = GetWorkflowStateName(mostRecentEvent.OldState);
             newState = GetWorkflowStateName(mostRecentEvent.NewState);
         }

         var lastEditedBy = item.Fields["__Updated by"].Value;
       
         var mostRecentUpdate = string.Format(Translate.Text("Changed from <br /> <b>{0}</b> to <b>{1}</b>"),oldState,newState);
         var scSlash = "\\";
         workflowItems.Editor = lastEditedBy.Replace(scSlash, "").Replace("sitecore", "");
         workflowItems.LastChange = mostRecentUpdate + "<br /> by " + lastEditedBy;
         WorkflowEvent[] history = workflow.GetHistory(item);

         foreach (Sitecore.Workflows.WorkflowEvent work in history)
         {
             workflowItems.Comments += work.CommentFields["Comments"] + " <br />";
         }

         workflowItems.Version = item.Version.Number.ToString();
         workflowItems.PublishingStartDate = item.Fields["__Valid from"].Value == String.Empty
             ? ""
             : DateUtil.IsoDateToDateTime(DateUtil.IsoDateToUtcIsoDate(item.Fields["__Valid from"].Value))
                 .ToShortDateString() + " "
               +
               DateUtil.ToServerTime(DateUtil.IsoDateToDateTime(item.Fields["__Valid from"].Value)).ToShortTimeString();
         workflowItems.RelatedPath = relatedPath;
         workflowItems.ItemGuid = item.ID.ToString();        
         workflowItems.CurrentWorkflowState = workFlowState;

         return workflowItems;
     }

     private  IEnumerable<MyWorkflowItems> GetItems(WorkflowState state, IWorkflow workflow, WorkflowProvider workflowProvider)
     {
         List<MyWorkflowItems> workflowItemsList = new List<MyWorkflowItems>();
         var currentWFstate = GetWorkflowStateName(state.StateID);
         DataUri[] items = workflow.GetItems(state.StateID);
         if (items != null)
         {
             int itemCount = 1;
             foreach (DataUri index in items)
             {
                 Item item = MasterDb.Items[new Sitecore.Data.ID(index.ItemID.ToString())];

                 if (item != null)
                 {
                     var theWorkFlowState = workflowProvider.GetWorkflow(item);
                     var itemWorkFlowState = FieldIDs.WorkflowState.Guid.ToString();
                     if (!string.IsNullOrEmpty(currentWFstate))
                     {
                         MyWorkflowItems workflowItem = GetWorkflowItem(item, workflow, itemCount, "", currentWFstate);
                         workflowItemsList.Add(workflowItem);

                         ++itemCount;
                     }
                 }
                 else
                 {
                     var missingItemID = index.ItemID;
                 }
             }
         }

         return (IEnumerable<MyWorkflowItems>)workflowItemsList;
     }


 public static bool ExecuteWorkflowCommand(Item item, string workflowEnum, string comment, string workflowCommandName)
        {
            IWorkflowProvider workflowProvider = item.Database.WorkflowProvider;
            if (workflowProvider == null)
            {
                Sitecore.Diagnostics.Log.Info("WorkflowProvider is null", "KB error log");
                return false;
            }

            var intSelected = (MyWorkflowStates)Enum.Parse(typeof(MyWorkflowStates), workflowEnum);
            var workflowID = System.Convert.ToInt16(GetWorkflowStateID(intSelected));         
            var workflowCommandID = GetCommandForWorkflow(workflowID, workflowCommandName);

            IWorkflow workflow = workflowProvider.GetWorkflow(workflowID);
            //COMMAND means action not TYPE so children of each workflowstate

            try
            {
                WorkflowResult workflowResult = workflow.Execute(workflowCommandID, item, comment, false);

                if (workflowResult != null)
                    return workflowResult.Succeeded;
            }
            catch (Sitecore.Exceptions.WorkflowStateMissingException ex)
            {
                Sitecore.Diagnostics.Log.Error(ex.Message + " " + ex.InnerException, "KB error log");
                return false;
            }
            return false;
        }

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.