JQuery for Everyone: Print (Any) Web Part
Back in August, I worked on a script to print web parts to answer a question from Stump the Panel. However, my script was not dynamic enough to adjust to the web parts on the page and create the “print this” links on the fly–I had no good ideas to tackle that problem.
The more I explore jQuery, the more ideas I get about solving old problems. So when someone else asked for a printing solution, I decided to tackle it in jQuery.
<script type="text/javascript" src="http://www.google.com/jsapi"></script> <script type="text/javascript"> // Load jQuery google.load("jquery", "1.2.6"); </script> <script type="text/javascript"> $(function(){ //find all web parts by their TD var tags = $("td[id^='MSOZoneCell_WebPart']"); //loop through web parts tags.each(function(){ //get id for print function var tagid = $(this).attr("id"); //append an image with onClick event $(this).find("h3 nobr").append( "<span style='padding-left:20px;'>"+ " <a href='#' onClick='printWebPart("+'"'+tagid+'"'+"); return false;'>"+ " <img src='/_layouts/images/nws16.gif' border='0' alt='printer friendly' />"+ " </a>"+ "</span>" ); }); }); function printWebPart(tagid){ if (tagid) { //build html for print page var html = "<HTML>\n<HEAD>\n"+ $("head").html()+ "\n</HEAD>\n<BODY>\n"+ $("#"+tagid).html()+ "\n</BODY>\n</HTML>"; //open new window var printWP = window.open("","printWebPart"); printWP.document.open(); //insert content printWP.document.write(html); printWP.document.close(); //open print dialog printWP.print(); } } </script>
Update 12/11/2008:
Dave T requested a version of this script that works with the List View Web Part (no title displays). Great idea Dave! Replace the main function with this one, notice there is a second “append” process that looks for the toolbar space instead of the title area.
$(function(){ //find all web parts by their TD var tags = $("td[id^='MSOZoneCell_WebPart']"); //loop through web parts tags.each(function(){ //get id for print function var tagid = $(this).attr("id"); //append an image with onClick event $(this).find("h3 nobr").append( "<span style='padding-left:20px;'>"+ " <a href='#' onClick='printWebPart("+'"'+tagid+'"'+"); return false;'>"+ " <img src='/_layouts/images/nws16.gif' border='0' alt='printer friendly' />"+ " </a>"+ "</span>"); $(this).find("td.ms-toolbar[width='99%']").append( "<span style='padding-left:20px;'>"+ " <a href='#' onClick='printWebPart("+'"'+tagid+'"'+"); return false;'>"+ " <img src='/_layouts/images/printerfriendly.gif' border='0' alt='printer friendly' />"+ " </a>"+ "</span>"); }); });
- JQuery for Everyone: Accordion Left Nav
- JQuery for Everyone: Print (Any) Web Part
- JQuery for Everyone: HTML Calculated Column
- JQuery for Everyone: Dressing-up Links Pt1
- JQuery for Everyone: Dressing-up Links Pt2
- JQuery for Everyone: Dressing-up Links Pt3
- JQuery for Everyone: Cleaning Windows Pt1
- JQuery for Everyone: Cleaning Windows Pt2
- JQuery for Everyone: Fixing the Gantt View
- JQuery for Everyone: Dynamically Sizing Excel Web Parts
- JQuery for Everyone: Manually Resizing Web Parts
- JQuery for Everyone: Total Calculated Columns
- JQuery for Everyone: Total of Time Differences
- JQuery for Everyone: Fixing Configured Web Part Height
- JQuery for Everyone: Expand/Collapse All Groups
- JQuery for Everyone: Preview Pane for Multiple Lists
- JQuery for Everyone: Preview Pane for Calendar View
- JQuery for Everyone: Degrading Dynamic Script Loader
- JQuery for Everyone: Force Checkout
- JQuery for Everyone: Replacing [Today]
- JQuery for Everyone: Whether They Want It Or Not
- JQuery for Everyone: Linking the Attachment Icon
- JQuery for Everyone: Aspect-Oriented Programming with jQuery
- JQuery for Everyone: AOP in Action - loadTip Gone Wild
- JQuery for Everyone: Wiki Outbound Links
- JQuery for Everyone: Collapse Text in List View
- JQuery for Everyone: AOP in Action - Clone List Header
- JQuery for Everyone: $.grep and calcHTML Revisited
- JQuery for Everyone: Evolution of the Preview
- JQuery for Everyone: Create a Client-Side Object Model
- JQuery for Everyone: Print (Any) Web Part(s) Plugin
- JQuery for Everyone: Minimal AOP and Elegant Modularity
- JQuery for Everyone: Cookies and Plugins
- JQuery for Everyone: Live Events vs. AOP
- JQuery for Everyone: Live Preview Pane
- JQuery for Everyone: Pre-populate Form Fields
- JQuery for Everyone: Get XML List Data with OWSSVR.DLL (RPC)
- Use Firebug in IE
- JQuery for Everyone: Extending OWS API for Calculated Columns
- JQuery for Everyone: Accordion Left-nav with Cookies Speed Test
- JQuery for Everyone: Email a List of People with OWS
- JQuery for Everyone: Faster than Document.Ready
- jQuery for Everyone: Collapse or Prepopulate Form Fields
- jQuery for Everyone: Hourly Summary Web Part
- jQuery for Everyone: "Read More..." On a Blog Site
- jQuery for Everyone: Slick Speed Test
- jQuery for Everyone: The SharePoint Game Changer
- JQuery For Everyone: Live LoadTip
Is there any way to set the print properties using jquery? I need to print a specific table in landscape so I capture all the data I need to display and would like to have it so my end users don’t have to change their print settings each time they try and print the list.
Awesome job, thanks for sharing!!
I was able to remove Print Icon and Title by including css media style inline to my theme css file.
@media print {
.ms-WPHeader TD {display:none;}
}
This with a DVWP I now have report printing without sql reporting services,
Thanks so much…
I am attempting to use your code to add a print icon on a web part that is a list view. I entered in the code via the content editor. In edit mode, I can see the print icon. However when I exit from edit mode, the icon dissapears. I think i need to link to the web part id? If so how do I find the web part id in SharePoint? Thanks! Bill
Hi I am also having this problem. I see it when I am in edit mode, but when I exit out of there it disappears. Please help. This would be awesome to use. Thanks. LD Davis
We are using MOSS and would like to be able to use ‘Print (Any) Web Part’ and ‘LoadTip Live on the same calendar. So far ‘No Luck’, is this possible? On the calendar(s) have requests for Color, Weekdays only, LoadTip Live and Print Any Web Part. We are using the calendars as our Hospital ‘Training Calendar’ and for the 12 ‘Conference’ Rooms. The calendars get posted on the door of each conference room.
Thanks,
Frank
Can you link to the older code with the hard-coded reference to the specific WebPart to be printed?
Thanks.
Thanks for this….
We get a print icon (page pic) on every line on the form, is this what we would expect to? This icon also prints out on the printed page.
Thanks Tamms