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
December 5, 2011 at 10:23 am |
Hmm missing the mouseover preview.. have not looked in “TruncateTextInListView.js” so maybe it is there …
December 5, 2011 at 7:23 pm
HI,
You are not supposed to edit that file.
Ensure you changed the “src” to the scripts in the CEWP code.
Alexander
December 5, 2011 at 5:07 pm |
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.
December 5, 2011 at 7:24 pm
Please double check the script “src” in the CEWP.
If you still get nothing, send me some screenshots.
Alexander
December 8, 2011 at 5:31 pm
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?
December 8, 2011 at 6:26 pm
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!
December 8, 2011 at 7:47 pm
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
December 6, 2011 at 10:25 pm |
This is perfect. Thanks alex!
December 16, 2011 at 4:04 pm |
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.
December 30, 2011 at 10:09 am
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
January 2, 2012 at 9:16 pm |
[...] Truncate text in list view and add mouseover preview [...]
January 2, 2012 at 9:18 pm |
[...] Truncate text in list view and add mouseover preview [...]
January 23, 2012 at 7:27 pm |
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!
January 23, 2012 at 7:34 pm
Sorry not sure why the code didn’t paste in…I can send a screen shot of it if that helps.
January 28, 2012 at 1:00 am
Hi,
Unless your field name is “MultiLineText”, you must change the argument in the function call.
Alexander
January 27, 2012 at 1:40 pm |
Hi Alex,
When i add referrence to TruncateTextInListView.js, it starting throwing error “Internet Explorer can not display the page”.
Please help.
January 28, 2012 at 1:03 am
Hi,
Which version of IE are you using? – and is it anything special with your setup? – like citrix or virtual machine?
Alexander
January 28, 2012 at 9:05 am |
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
January 28, 2012 at 11:04 am
Hi,
Try to change line 22-24 from
$(document).ready(function(){ init_TruncateText(); });to
_spBodyOnLoadFunctionNames.push('init_TruncateText');Alexander
January 29, 2012 at 9:45 am
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″
January 31, 2012 at 6:39 pm
Hi Alex,
Can you please suggest something else too… if that will work…
January 29, 2012 at 12:15 pm
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
January 29, 2012 at 7:44 pm
Now no javascript error but nothing is working…
Neither truncating and nor mouse hover working…
February 9, 2012 at 8:49 pm
Hi,
If you add an alert inside the $(document).ready(function(){…});
Does it fire?
Alexander
February 10, 2012 at 9:32 am
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();
});
February 19, 2012 at 6:43 pm
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
April 16, 2012 at 11:44 pm |
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?
April 18, 2012 at 10:07 pm
A multi line text field must be addressed by the display name. The quotes does not look right – try changing them to double quotes.
If this does not work, try pasting the full code from your CEWP here – look her to learn how to post code in comments
Alexander