SharePoint Data View XSLT Tips and Tricks – Limit text field

SharePoint Data View XSLT Tips and Tricks – Limit text field

Here is an XSLT syntax used to limit the number of characters of a text column (in this example the title field), In case the text is more than 50 characters long this function will substring it from the start and up to its last occurrence of a white space and add 3 points after it –

1
2
3
4
5
6
7
8
<xsl:choose>
  <xsl:when test="string-length(@Title) &gt; 50">
    <xsl:value-of select="concat(substring(@Title, 1, 49 + string-length(substring-before(substring(@Title, 50),' '))),'...')" />
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="@Title" />
  </xsl:otherwise>
</xsl:choose>

If you don’t mind your text being cut off after the limited number of characters you could use this shorter XSLT syntax –

1
2
3
4
5
6
7
8
<xsl:choose>
  <xsl:when test="string-length(@Title) &gt; 50">
    <xsl:value-of select="concat(substring(@Title, 1, 50),'...')" />
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="@Title" />
  </xsl:otherwise>
</xsl:choose>

To count how many characters can fit your designated area you can use this nice online tool.

This is a cool trick to use, but if your text field should fit into one row only, you shouldn’t bother with it and it will be smarter to use this CSS Truncate String with Ellipsis technique which is supported in almost all browsers (even by IE8 believe it or not!).

SharePoint List Validation Formula for Special Characters and string length

SharePoint List Validation Formula for Special Characters

Let me start off by saying I don’t recall ever using the SharePoint out of the box list validation, but yesterday when I needed to I searched the web and was surprised to see there aren’t many examples and best practices for using it out there, so I thought sharing this might be nice.
First of all in order to add a validation rule go to your SharePoint list or library settings and click on Validation settings –

Validation Settings

Validation Settings

Validation Settings

Secondly it’s confusing and important to understand that in order for your validation rule to work (meaning a message will be displayed for the user and the form won’t be submitted) your formula must return ‘TRUE’.


Here are the formulas that I recently used:

A validation formula for the number of string characters (Useful for the OOTB Title column for which you cannot limit the number of characters) –
=LEN(Title) < 45

A validation formula that checks for special characters (e.g. if the title contains a comma) –
=ISERROR(FIND(“,”,Title))

Important Note: The ISERROR syntax is used here like a “NOT” rule.

And now for combining a few validation rules together – in the following formula if the title is longer than 45 characters OR contains a comma OR contains an apostrophe, the validation message will be displayed and the form will not be submitted -

=AND(LEN(Title)<45,ISERROR(FIND(“,”,Title)),ISERROR(FIND(“‘”,Title)))

Validation Formula

Validation Formula

Validation Formula

Important note: It’s confusing but we must use “AND” for combining validation rules if we want our validation to work in case one or another of them is violated, that’s because if the entire expression will return a “FALSE” value our validation won’t work… again like I said it’s a bit confusing, hope you get it

Validation Message

Validation Message

Validation Message