2

I have written the following code to sort the left hand side of multilist using code:

public Item[] ListQuery(Item item)
{
    try { 
        List<Item> sourceItems = new List<Item>();

        item = Sitecore.Context.ContentDatabase.GetItem("/sitecore/content/website/Home/newsroom");

        List<Item> otherCampaigns = item
            .GetChildren()
            .Where(x => x.TemplateID.ToString().Equals("{D5016838-1EC3-476A-9270-F9995C57AD5B}"))
            .OrderByDescending(x => x[Sitecore.FieldIDs.Updated])
            .ToList();

        sourceItems.AddRange(otherCampaigns);
        return sourceItems.ToArray();
    }
    catch
    {
        return new Item[0];
    }
} 

I am getting the correct order of sorted items in the code but in the left hand side of multilist field it is not populating in the same order as in "sourceItems" in the code.In multilist field the items are coming in any random order.Kindly assist

1

My initial answer here assumed that Sitecore would actually use the order in the array resulting from MultilistEx's GetItems method, but that would have been too simple. I tested it just after answering and found that it didn't work. So I had to fix my errors.

What you want to do (and probably have done) is override Sitecore MultilistEx. This has the method GetSelectedItems, which finds all the items to render in the right side of the Multilist, but at the same time (for some reason) sorts all items in the left side alphabetically, and returns a SortedList (IDictionary) as output parameter.

You could either override the entire implementation of GetSelectedItems or DoRender - but I've selected to do like below, and override GetSelectedItems, calling base and then create a new SortedList with a custom descending comparer, like this:

namespace MyNamespace
{
    public class MyMultilist : global::Sitecore.Shell.Applications.ContentEditor.MultilistEx
    {
        protected override void GetSelectedItems(Item[] sources, out ArrayList selected, out IDictionary unselected)
        {
            base.GetSelectedItems(sources, out selected, out unselected);
            var resorted = new SortedList<string, Item>(new DescendingComparer());
            foreach (DictionaryEntry entry in unselected)
            {
                var item = (Item)entry.Value;
                var sortKey = MainUtil.GetSortKey(item[global::Sitecore.FieldIDs.Updated]);
                resorted.Add(sortKey, item);
            }
            unselected = resorted;
        }

        protected class DescendingComparer : IComparer<string>
        {
            public int Compare(string x, string y)
            {
                return Comparer<string>.Default.Compare(x, y) * -1;
            }
        }
    }
}

Once implemened and configured (add controlsource to config file and create the fieldtype in core database) this worked for me.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.