I need to view the number of selected items in a MultiList field using out of the box SXA (1.7) components(i.e. no custom coding).

I tried doing this with a rendering variant NVelocity template using $item.List. This returned a string with the id's of the items separated by | .

enter image description here

I also would rather not use javascript. Any out of the box solution like NVelocity or suggestion without custom coding?

  • Did you try $item.List.Count() ? (didn't try myself, but that would be my first guess) – Gatogordo Dec 18 '18 at 14:07
  • i did, i also tried size and length. but its a string so it didn't work. – Menah Alasmy Dec 18 '18 at 14:15
  • $item.List.Count(c => c == '|') + 1 ? Or $item.List.Count(c => c == '{').. – Gatogordo Dec 18 '18 at 14:22
  • Thank you for your help , but Is this c# code ? because that didn't work in the rendering variant. – Menah Alasmy Dec 19 '18 at 7:30

I don't think there is an OOTB NVelocity function that can handle splits and counts.

So.. one option would be to write an NVelocity function - which is not that hard, but does require custom code and that is not what you wanted.

But I did find a way to do this in NVelocity without a custom function (note that a custom function would make this code cleaner and more reusable):

#set ($counter = 0)
#foreach($character in $item.List)
#if ($character.ToString() == "{")
#set ($counter = $counter + 1)
Length: $counter

The last line will display your number of items - adapt that one as needed..

Like I mentioned - if you need this at more than one spot: go for a custom function (example here by Michael West). Also not sure about the performance of this code.. but it does work.

  • Don't forget to mark answers as accepted when they are sufficient for you (I've noticed that you haven't done this for your other questions either) ;) – Gatogordo Dec 19 '18 at 13:01

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.