1

I have two Sharepoint lists. In List 1, I have a lookup column with additional fields linked to List 2. I am able to format the lookup column itself but I dont know how to do it for additional fields.

They are always shown as Value 1; Value 2; Value 3

Column name of my additional field is "TrackingNumbers_x003a_ShippedTo" So TrackingNumbers is the lookupField (it shows it as "Field=Tracking0" at the address bar for some reason).

Update:

Thanks to Chris, I was able to accomplish the look I wanted. Ok I wasnt able to get rid of ";" semi-colons but at least now they each appear on new line so they are aligned with tracking numbers lookup column. So in short "@currentField" is how you reference to lookup column additional fields but as Chris says they come as single string. I didnt bother to remove semi-colons because it has limitations Chris mentions. The formulas get too complicated and you can show limited number of values with formatting.

  {
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "debugMode": true,
  "style": {
    "flex-direction": "column",
    "align-items": "flex-start",
    "margin-top": "2px",
    "padding-right": "0px",
    "width": "100%",
    "overflow": "hidden"
  },
  "children": [
    {
      "elmType": "div",
      "txtContent": "@currentField",
      "style": {
        "color": "#333333",
        "vertical-align": "middle",
        "width": "24px"
      }
    }
  ]
}

2 Answers 2

2

Multi-Lookup fields (more than one lookup value) can be formatted. The values come back as an array which lets us use the advanced formatting forEach option. Unfortunately, projected fields for any multi select column as primary won't come back as an array. Instead, the values come back as a single string with a separator (;). While you can still apply formatting, you can't deal with each value individually with the forEach as before.

I've provided additional details and a sample implementation of how you might solve this on this answer: https://sharepoint.stackexchange.com/a/290961/2881

In summary, we have to get creative with a series of conditional elements that are extracting values using a complex combination of if, substring, and indexOf operators. This approach does work but has some drawbacks:

  • It's super complex to read/write
  • It can only handle a fixed set of values with each value having increasingly complex formulas to extract those values

I have put together a sample that illustrates how to handle both the multi-lookup column itself and any additional columns (projected fields). You can find it as part of the M365 PnP List-Formatting repo here: https://github.com/pnp/List-Formatting/tree/master/column-samples/multi-lookup-projected-field

4
  • 1
    Hi Chris, I appreciate. I follow your tutorials closely but this one I missed. Basically what I am trying to achieve is to match tracking numbers with their courier names. Tracking numbers are multi value lookup each on separate line. Then I need couriers (lookup additional field) exactly like tracking numbers each on separate line so first tracking number will correspond to the first courier name. I will look at your tutorial. Can I ask questions if I get stuck? Commented Apr 21, 2021 at 19:15
  • 1
    I've updated my answer with a link to a sample I've built that should hopefully help address your questions. If the formats provided in the sample are applied to your columns then things should line up. You could also add more styles such as borders, colors, etc. to help line things up. Absolutely, you can ask more questions! This is an advanced scenario and can get pretty complicated. Commented Apr 22, 2021 at 17:07
  • 1
    Hi Chris, due to limitations (I dont know how many entries lookup field can have), I decided to just line them up without trying to remove the semi-colon. I have updated my question with how I did after removing lines from your example. Thank you! I hope Microsoft comes up something more usable in the future... Commented Apr 23, 2021 at 6:50
  • It looks like a split function was added but of course no real examples of how to actually get the items out of the returned array. Any chance you have any insights on what to try? I have tried a LOT ;) Commented Jan 5, 2023 at 11:57
0

I guess those limitations are now solved by the new split function. There was a workaround though, consisting in using calculated fields as secondary columns, so that the values are given the same string lenght with additionnal signs (for example : "hello^^^^;goodbye^^;Michelle^;..."). It still came back as single string, but the values being of equal length between separators, it was possible to isolate the relevant value to the current loop, using loopindex and substring. Once the relevant value isolated, the additionnal signs ("^" for example) could be removed.

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.