LibrarySites.Banner

Adding a Workflow State Facet

A common request we are seeing through our support channel is to add a facet that shows workflow states for items. This is a nice way to not have to switch over to the workbox to see which content is in which state. In fact we even added a dynamic quick action in the search results screen that allows you to propagate items through workflow. Let's start by defining what we need to do.

1: We need to make sure that the Workflow State field is in the index. This is stored in Sitecore in raw format as a GUID. Hence, we have two decisions. We can resolve the friendly name of the GUID at query time OR we can add a computed field that stores the name of the Workflow State and we simply facet on that.

2: Let's take the computed field decision. It is cleaner, more scalable and we don't take any hits at query time as everything is already prepared in the index. We just need to make sure that when we are setting up workflows that we never call the states exactly the same thing or this will mess with our facets.

3: We then need to define a facet through the UI.

4: We then need to put some items in workflow to test it out.

Computed Fields

public class WorkflowState : IComputedIndexField
{
    /// <summary>
    /// Gets or sets the field name.
    /// </summary>
    public string FieldName { get; set; }

    /// <summary>
    /// Gets or sets the return type.
    /// </summary>
    public string ReturnType { get; set; }

    /// <summary>The compute field value.</summary>
    /// <param name="indexable">The indexable.</param>
    /// <returns>The <see cref="object" />.</returns>
    public object ComputeFieldValue(IIndexable indexable)
    {
        Item item = indexable as SitecoreIndexableItem;

        if (item == null)
        {
            return null;
        }

        var database = item.Database;

        if (database == null)
        {
            return false;
        }

        if (database.WorkflowProvider != null)
        {
            var wf = database.WorkflowProvider.GetWorkflow(item);
            if (wf != null)
            {
                //You can add extra defensive checks here if you want - I live dangerously
                return wf.GetState(item).DisplayName
            }
        }

        return false;
    }
}

Configuration

Add the following line into the ComputedFields part of your config.

<field fieldName="workflowstate"              storageType="no"  indexType="tokenized">Custom.ComputedFields.WorkflowState,Custom.Code</field>

Rebuild your index

Rebuild you index through the Desktop -> Control Panel -> Indexing -> Index Manager

Add the facet to show through the UI

Navigate to /sitecore/system/settings/Buckets/Facets and add a new facet item. Set the "field" field to "workflowstate" i.e. the name of your computed field. Check the checkbox for global. This tells the system to run this facet no matter where you run the search from in the content tree.

Place items in workflow

Before you run a search, make sure you have items in a workflow so that the facets show up.

Dev Team