Truncate text in list view and add mouseover preview


08.12.2011 I have updated to v1.1 to fix a bug preventing the use of FieldInternalName for addressing the fields. Thanks to Ed MacIntosh (@cosmo132) for finding the bug.


I have previously done this article Multi line text field in list view: Shorten text and add hovereffect to view full text.

This post is an updated version to support both SharePoint 2007 and SharePoint 2010.

Features

This solution will shorten any text column (single line or multi line) in a list view to a preset length, and append “…” to indicate that the text is truncated. If the text is shorter than the “trigger length”, the cell is skipped.

How to set up

Go get jQuery v1.6.4 from here.
Note that this is NOT the latest version. I have not had the time to find out why v1.7+ does not work.

Download the code for the file “TruncateTextInListView.js” from here Note the different versions for SharePoint 2007 and SharePoint 2010.

Upload both files to a shared document library – or a folder in the root of the site (created with SharePoint Designer). Ensure all users have read access to the location.

Put a CEWP below the list view web part, and insert this code:

<script type="text/javascript" src="/test/English/Javascript/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="/test/English/Javascript/TruncateTextInListView.js"></script>
<script type="text/javascript">
	
function init_TruncateText(){
	truncateText('MultilineText',50,'');
}

</script>

Note:
You must change the “src” attribute of the script tags to point to your local copies of jQuery and “TruncateTextInListView.js”.

Parameters for the function “truncateText”

  • fieldName: The FieldInternalName for single line text fields or display name for Multi line text fields.
  • initialLength: The number of character to display.
  • inPlaceHoverText: This argument is optional and can be a text to show in place of the original text – like “hover to read”.

Ask if something is unclear.

Alexander

28 Responses to “Truncate text in list view and add mouseover preview”

  1. nicodejong (@nicodejong) Says:

    Hmm missing the mouseover preview.. have not looked in “TruncateTextInListView.js” so maybe it is there …

    • Alexander Bautz Says:

      HI,
      You are not supposed to edit that file.
      Ensure you changed the “src” to the scripts in the CEWP code.

      Alexander

  2. Ed MacIntosh (@cosmo132) Says:

    using SharePoint 2007 and I am not able to get this to run at all. tried single line of text field referenceing the fieldinternal name and display naem also tried with a multiline of text field refenrenceing field internal name as well as display name.

    • Alexander Bautz Says:

      Please double check the script “src” in the CEWP.

      If you still get nothing, send me some screenshots.

      Alexander

    • Ed MacIntosh (@cosmo132) Says:

      Thanks Alex,

      Working fine now. Not sure what iwas doing wrong.

      Is there a way to have this apply to 2 or more list columns on the same listview?

    • Ed MacIntosh (@cosmo132) Says:

      Nevermind I got it.

      I did see that the display name works for both single line of text as well as multiple lines of text. Reason I could not get it to work earlier was that I was referencing the internal field name. Thanks again. This is a great webpart!

    • Alexander Bautz Says:

      Thanks for pointing this bug out – I guess i was in a hurry when i posted this code and did not test it thoroughly.

      See updated code.

      Alexander

  3. Nick D Says:

    This is perfect. Thanks alex!

  4. chintan Says:

    Hi Alex,

    I tried your given script but got fail. I am using MOSS. It seems URL may be the problem. I created document library (JavaScript) in my site and gave URL as below in CEWP.
    src=”/JavaScript/jquery-1.7.1.min.js”.

    I also created Multi line column with Plain and Rich text type.

    Pls advice.

    • Alexander Bautz Says:

      Hi,
      As i wrote in the article, you must NOT use jQuery 1.7x as this is not supported. Go get v1.6.4 and see if that helps.

      Alexander

  5. SharePoint 2010: Recopilatorio de enlaces interesantes (XXVII)! - Blog del CIIN Says:

    [...] Truncate text in list view and add mouseover preview [...]

  6. SharePoint 2010: Recopilatorio de enlaces interesantes (XXVII)! « Pasión por la tecnología… Says:

    [...] Truncate text in list view and add mouseover preview [...]

  7. Sara Says:

    Hi Alexander,

    Can you help me troubleshoot?

    I downloaded jQuery v.1.6.4 and the the code for the file “TruncateTextInListView.js”, uploaded both to the Shared Documents Library on the site. I added the list to a new page and the CEWP below it. I pasted the following code into the HTML source of the CEWP:

    function init_TruncateText(){
    truncateText(‘MultilineText’,50,”);
    }

    I also tried uploading this code as a .txt file and adding a link to the file from the CEWP. And I tried it in a different library (in case the “Shared%20Documents” part of the link was messing things up).

    The list in question is a Links List. I changed the title of the multi-line Rich Text column to Summary instead of it’s original title Comments.

    Any ideas why it’s not working? Wondering about maybe the jquery download because I’m not sure how to test if that’s working.

    Thanks!

    • Sara Says:

      Sorry not sure why the code didn’t paste in…I can send a screen shot of it if that helps.

    • Alexander Bautz Says:

      Hi,
      Unless your field name is “MultiLineText”, you must change the argument in the function call.

      Alexander

  8. Arun Goel Says:

    Hi Alex,

    When i add referrence to TruncateTextInListView.js, it starting throwing error “Internet Explorer can not display the page”.

    Please help.

    • Alexander Bautz Says:

      Hi,
      Which version of IE are you using? – and is it anything special with your setup? – like citrix or virtual machine?

      Alexander

  9. Arun Says:

    Hi, I am using 8.0.6001.18702…
    No, nothing special.. i have two web server and 1 db server and 1 index server….
    In IE 6 or 7 it is throwing an error Operation Aborted and then Internet explorer can not display the page
    In IE 8, it is throwing two java script errors…
    1. HTML Parsing error. Unable to modify the parent container element before the child element is closed. Line 0
    2. Object expected. Line 11 char 2

    • Alexander Bautz Says:

      Hi,
      Try to change line 22-24 from

      $(document).ready(function(){
      	init_TruncateText();
      });
      

      to

      _spBodyOnLoadFunctionNames.push('init_TruncateText');
      

      Alexander

    • Arun Says:

      Hi Alex,

      It did not work too :(
      it is still throwing java script error “HTML Parsing error. Unable to modify the parent container element before the child element is closed(KB927917). Line 0 char 0″

    • Arun Says:

      Hi Alex,

      Can you please suggest something else too… if that will work… :(

    • Alexander Bautz Says:

      Hi,
      Try to move this line:

      $("body").append(cssBuffer.join(''));
      

      Into the document ready block like this:

      $(document).ready(function(){
      	$("body").append(cssBuffer.join(''));
      	init_TruncateText();
      });
      

      Alexander

    • Arun Says:

      Now no javascript error but nothing is working… :(
      Neither truncating and nor mouse hover working…

    • Alexander Bautz Says:

      Hi,
      If you add an alert inside the $(document).ready(function(){…});

      Does it fire?

      Alexander

    • Arun Says:

      Hello Alex,

      this code throw an object reference error in IE 7 and no error in IE 8 but do not work as well.

      $(document).ready(function(){
      $(“body”).append(cssBuffer.join(”));
      init_TruncateText();

      });

      as u asked i add one alert above it, alert is getting called but then page throws object reference error.

      $(document).ready(function(){
      alert(‘hi’);
      $(“body”).append(cssBuffer.join(”));
      init_TruncateText();

      });

    • Alexander Bautz Says:

      Try REMOVING this from the “TruncateTextInListView.js” file:

      var cssBuffer = [];
      cssBuffer.push("<style type='text/css'>");
      cssBuffer.push("span.hideMe{");
      cssBuffer.push("visibility:hidden;");
      cssBuffer.push("}");
      cssBuffer.push("</style>");

      $("body").append(cssBuffer.join(''));

      then ADD this to the bottom of your CEWP code:

      <style type="text/css">
      span.hideMe{
      visibility:hidden;
      }
      </style>

      I’m not sure if this helps, but without being able to recreate this error it’s a bit difficult to pinpoint the cause.

      Alexander

  10. Kelly Schroeder Says:

    Hi Alexander,
    I’m trying to get this up and running on SP2010 Foundation. I have added the CEWP below my list. I have editted the src statements to point to my server using both relative and absolute pathing. I have grabbed 1.6.4 from jscript. I have looked up the FieldInternalName for my multiline field and put that in the CEWP. Not sure why I’m not getting any response from the system. Here is the text in the CEWP where Scripts is a document library in the same site:

    function init_TruncateText(){
    truncateText(‘Notes’,50,”);
    }

    Any thoughts?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 435 other followers