Displaying a Multi-Select Column "Nicely"

1 minute read

UPDATE 2010-08-26: I’ve added this template to the SPXSLT project on Codeplex. There’s a bit more explanation there.

UPDATE 2010-04-27: Shalin Parmar pointed out in a comment below that I had  a bug in the template where the separator would only be displayed between the first and second values.  I’ve made a change to the template to fix this as well as to allow markup in the separator.  Thanks, Shalin!

Here’s another little utility piece of XSL which I have used from time to time.  It takes as its parameters the multi-select column’s value and a separator string.  The template will replace every occurrence of the semi-colon (;) in the multi-select value with the separator string.  This is another example of what you can pull off with recursion in your XSL templates.

Instead of seeing something like this:

value1;value2;value3

if you pass in ‘ | ‘ as the separator, you’ll see

value1 | value2 | value3

<xsl:template name="MultiSelectDisplay">
  <xsl:param name="MultiSelectValue"/>
  <xsl:param name="MultiSelectSeparator"/>
  <xsl:choose>
    <xsl:when test="contains($MultiSelectValue, ';')">
      <xsl:value-of select="concat(substring-before($MultiSelectValue, ';'), $MultiSelectSeparator)" disable-output-escaping="yes"/>
      <xsl:call-template name="MultiSelectDisplay">
        <xsl:with-param name="MultiSelectValue" select="substring-after($MultiSelectValue, ';')"/>
        <xsl:with-param name="MultiSelectSeparator" select="$MultiSelectSeparator"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$MultiSelectValue" disable-output-escaping="yes"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
Advertisements

85 Comments

  1. hi, i have one required with data view web part using linked data source

    linked data source have two lists ListA, ListB
    ListA and ListB have same look up column

    but one list accepts single selection
    another one accepts multiple selection

    I applied Grouping but it shows duplicate grouping names
    because ListA accepts single selection and ListB accepts multiple selection

    that way it shows same group name twice

    how i can solve this problem please help me

    Regards
    venkat Ambala
    venkateswarlu.ambala@hexacorp.com

    Reply
    • venkat:

      There’s not a simple answer to your question. You’ll need to determine the unique values in the multi-select and generate the groupings in your XSL. As you know, in multi-select columns, the data is simply stored with #; delimiter, so the “groups” will be redundant. You may be better off using some client side script.

      M.

      Reply
  2. So awesome. You are a SharePoint Genius! I had to tweak the code a little for the string in SharePoint 2013, but this works great!

    I always find your posts helpful and learned everything I know about xslt from reading them.

    Thanks!

    Reply
  3. I have same problem with “People and Group” column. I got ;# and it irritates me. I have tried all the solutions that have been provided by you guys … no success .. please help !

    Reply
  4. Dear Marc,
    the solution with “br” worked for me. however, i try tp show a second column of my lookup beside the first column.
    Like:
    Entry 1 | 100
    Entry 2 | 200
    Entry 3 | 300

    When im using your Call-code twice, the second information does not show up.
    I specified in my column-settings to “pull” the information also. (Add a column to show each of these additional fields) and i used this field. But it do not show up.

    Do you have a idea?

    Best Regards, Mike

    Reply
    • @Michael:

      The template as I wrote it will just split of the string you pass in for MultiSelectValue based on MultiSelectSeparator. It’s not “smart” enough to understand your “first” and “second” values.

      M.

      Reply

Have a thought or opinion?