Dhanuka777
Dhanuka777

Reputation: 8616

Sitecore workbox change sort order

By default Sitecore workbox displays the Item name, and sort the item list by Item name.

In one of my previous posts regarding this I managed to change the item name to a custom field.

Now I need to sort workbox by this field. How can I do this?

Upvotes: 4

Views: 676

Answers (2)

Dhanuka777
Dhanuka777

Reputation: 8616

After studying Sitecore workbox modifications, I came across with following solution.

Step 1 - Modify the GetItems method as follows,

    private DataUri[] GetItems(WorkflowState state, IWorkflow workflow)
    {  
        if (workflow != null)
        {
            var items = workflow.GetItems(state.StateID);                
            Array.Sort(items, new Comparison<DataUri>(CompareDataUri));
            return items;
        }
        return new DataUri[] { };
    }

Here comes the "CompareDataUri" method,

    private int CompareDataUri(DataUri x, DataUri y)
    {
        //Custom method written to compare two values - Dhanuka
        Item itemX = Sitecore.Context.ContentDatabase.GetItem(x);
        Item itemY = Sitecore.Context.ContentDatabase.GetItem(y);

        string m_sortField = "__Updated";
        bool m_descSort = false;

        var res = 0;

        res = string.Compare(itemX[m_sortField], itemY[m_sortField]);

        if (m_descSort)
        {
            if (res > 0)
                return -1;
            if (res < 0)
                return 1;
        }

        return res;
    }

This approach is optimized for performance.

Upvotes: 1

Marek Musielak
Marek Musielak

Reputation: 27132

Assuming that you already have your own implementation of the WorkboxForm as described in the post you linked in your question, you need to change the code of the DisplayState method.

The DataUri[] items inflow parameter of this method gives you the list of all items which are in given state of the workflows. You need to retrieve all the Sitecore items from this parameter and sort them:

DataUri[] items = new DataUri[0];
List<Item> sitecoreItems = items
    .Select(uri => Context.ContentDatabase.Items[uri])
    .OrderBy(item => item["YourCustomField"])
    .ToList();

And use the new list for selecting the current page items. This solution is not optimized for the performance - you need to get every item in given state from database so you can access the custom field.

Upvotes: 4

Related Questions