Looping through people in a PnP Modern Search result template

When retrieving multiple users using PnP Modern Search, you’ll get something like this:

{
  "DocumentOwnerOWSUSER": "[email protected] | User One | C2D1...A407 i:0#.f|membership|[email protected]\n\n[email protected] | User Two | C2D1...A407 i:0#.f|membership|[email protected]"
}

Unfortunately, using handlebars and handlebars-helpers, it’s not possible to split on the new lines (\n\n).

Instead, we can split | and hardcode the indexes to get the email and name.

{{#if item.DocumentOwnerOWSUSER}}
  <ul>
    {{#withGroup (after (split item.DocumentOwnerOWSUSER '|') 1) 4}}
      <li>
        <picture>
          <img
            alt='Photo for {{itemAt this 0}}'
            height='24'
            loading='lazy'
            src='/_layouts/15/userphoto.aspx?size=L&username={{getUserEmail (itemAt this 3)}}'
            width='24'
          />
        </picture>
        <span>{{itemAt this 0}}</span>
      </li>
    {{/withGroup}}
  </ul>
{{else}}
  (None)
{{/if}}

Related posts