SharePoint XSL String Replacement Function - SharePoint Development & Administration + InfoPath

Sunday, March 22, 2009

SharePoint XSL String Replacement Function

Creating a list view Web Part to display data from a look-up field containing multiple look-ups.

I ran into a problem when creating a list view Web Part in SharePoint Designer that displays data from look-up fields. If the field only allowed a single look-up, the list view was easy to create with no customisation required for the Web Part to display properly.

After adjusting the look-up column/field to allow multiple selections, the result was an "Unable to display Web Part" message. To allow the multiple selection look-ups to be listed I needed to customise the XSL template to list each value in the look-up field.

The following XSL template I adapted from, which solved my problem.

The data from the multiple selection lookup field is returned in a string format, with each selection delimited with a ";". The following function replaces each occurance of the first string ";" with the entire second string
. The result is a line break between each value, instead of the ";". I then adjusted the template to add "- " before each value.

Output without template:

Lookup Value 1; Lookup Value 2

Output using template below:

- Lookup Value 1
- Lookup Value 2

<xsl:variable name="myString" select="@Todays_x0020_Tasks"/>

<xsl:call-template name="replaceCharsInString">
<xsl:with-param name="stringIn" select="string($myString)"/>

<xsl:with-param name="charsIn" select="';'"/>

<xsl:with-param name="charsOut" select="'&lt;br&gt;'"/>

<xsl:template name="replaceCharsInString">
<xsl:param name="stringIn"/>

<xsl:param name="charsIn"/>

<xsl:param name="charsOut"/>

<xsl:when test="contains($stringIn,$charsIn)">
<xsl:value-of select="'- '"/>

<xsl:value-of select="concat(substring-before($stringIn,$charsIn),$charsOut)" disable-output-escaping="yes" />

<xsl:call-template name="replaceCharsInString">
<xsl:with-param name="stringIn" select="substring-after($stringIn,$charsIn)"/>

<xsl:with-param name="charsIn" select="$charsIn"/>

<xsl:with-param name="charsOut" select="$charsOut"/>

<xsl:value-of select="'- '"/>

<xsl:value-of select="$stringIn"/>

Share this article:
Stumble This Delicious
submit to reddit


David from Texas A&M said...

Thanks for the post, it has helped me with half my problem! Do you know how to add the code to pull in the href value for each of LookUp value? I need users to be able to click on the LookUp values. - david from Texas A&M

DanielS said...

To make each lookup link to the item, try the following:

1. Using SharePoint Designer, Click the title of one of the lookups in the dataview (after it has been converted to "- lookupname" using the string replace XSLT function)

2. Make sure the field is formatted as "Rich Text".

3. Right Click on the same lookup value and select "Hyperlink..."

4. On the Edit Hyperlink dialog, make sure the "Text to Display" is set to "{concat(substring-before($stringIn,$charsIn),$charsOut) }".

5. Set the Address field of the hyperlink to {ddwrt:URLLookup('{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}', 'lookup_x0020_field', string(@Title))}

- where "{XXXX....} is the ID of the list which contains the items the lookup field refers to

- lookup_x0020_field is the name of the lookup field containing the lookup pointing to a separate list (same field as what was customised by the string replace function)

- String(@Title) is the the name of the field which the lookup is mapped to. In this case, the url for the item is found by matching the "Title" field from an item in the separate list with the value of the current lookup.

Let me know how you go.



Marja said...

Hi Daniel,

Thanks for this post, it helped a lot especially the previous comment where you explain how to make a hyperlink to the lookup values.

However in the range of lookup values all of the lookup values get the href from the first one. So lookupvalue 2 refers to href=server/subsite/list/dispform.aspx?id=value of lookupvalue 1 instead of href=server/subsite/list/dispform.aspx?id=value of lookupvalue 2

Can you shine a light on this?
Our whole site ( is full with multiple lookups: if I can get this to work...that would be great!


Anand Balasubramanian said...

Thank you very much buddy !! - you saved my day. I am new to XSL technologies and your blog post helped me satisfy the same exact requirement that you were talking about :-)

Thanks Again!

Chaima said...

Hi ,
Please helpe me I need the same astuce but I don't know where I will put this code :-(

Post a Comment