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
  • Can you please share how you got the code: source to work within sitecore. I currently have the same functionality written however I am receiving a invalid source error when trying to utilise my custom logic. May 7 at 14:44

1 Answer 1

2

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.

2
  • I followed this code to replace an existing multilist field with a new custom datatype. It worked great in the content editor. However, Solr started indexing the data differently. It switched from indexing the value as an _sm to a _t. I tried adding the new datatype under the <fieldTypes hint="raw:AddFieldByFieldTypeName". But that didn't change anything. How can I get Solr/Sitecore to index my overridden Multilist datatype as an ID/Guid array? Nov 7, 2023 at 21:48
  • I figured out the config I was missing. I needed to add my new datatype under the fieldReaders section too, mapFieldByTypeName. I copied the multilist node and switched it to my new fieldTypeName. Nov 7, 2023 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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