Displaying a Multi-Select Column "Nicely"

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>

Similar Posts

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
    [email protected]

    1. 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.

  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!

    1. ClairePoint:

      I’m glad this post helped you out. I just write down the stuff I struggled with. That makes me a good documenter, not a genius. ;+)

      What did you have to change for SharePoint 2013?

      M.

  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 !

  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

    1. @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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.