1,804 articles and 15,030 comments as of Monday, May 30th, 2011

EndUserSharePoint has combined resources with NothingButSharePoint.com. You can now find End User (Mark Miller), Developer (Jeremy Thake) and IT Pro SharePoint content all in one place!

This site is a historical archive and is no longer being updated. Please update your favorites, bookmarks and RSS feeds.

NothingButSharePoint.com
Tuesday, December 9, 2008

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>");
    });
});

Paul Grenier

View all entries in this series: PaulGrenier-JQuery for Everyone»
Entries in this series:
  1. JQuery for Everyone: Accordion Left Nav
  2. JQuery for Everyone: Print (Any) Web Part
  3. JQuery for Everyone: HTML Calculated Column
  4. JQuery for Everyone: Dressing-up Links Pt1
  5. JQuery for Everyone: Dressing-up Links Pt2
  6. JQuery for Everyone: Dressing-up Links Pt3
  7. JQuery for Everyone: Cleaning Windows Pt1
  8. JQuery for Everyone: Cleaning Windows Pt2
  9. JQuery for Everyone: Fixing the Gantt View
  10. JQuery for Everyone: Dynamically Sizing Excel Web Parts
  11. JQuery for Everyone: Manually Resizing Web Parts
  12. JQuery for Everyone: Total Calculated Columns
  13. JQuery for Everyone: Total of Time Differences
  14. JQuery for Everyone: Fixing Configured Web Part Height
  15. JQuery for Everyone: Expand/Collapse All Groups
  16. JQuery for Everyone: Preview Pane for Multiple Lists
  17. JQuery for Everyone: Preview Pane for Calendar View
  18. JQuery for Everyone: Degrading Dynamic Script Loader
  19. JQuery for Everyone: Force Checkout
  20. JQuery for Everyone: Replacing [Today]
  21. JQuery for Everyone: Whether They Want It Or Not
  22. JQuery for Everyone: Linking the Attachment Icon
  23. JQuery for Everyone: Aspect-Oriented Programming with jQuery
  24. JQuery for Everyone: AOP in Action - loadTip Gone Wild
  25. JQuery for Everyone: Wiki Outbound Links
  26. JQuery for Everyone: Collapse Text in List View
  27. JQuery for Everyone: AOP in Action - Clone List Header
  28. JQuery for Everyone: $.grep and calcHTML Revisited
  29. JQuery for Everyone: Evolution of the Preview
  30. JQuery for Everyone: Create a Client-Side Object Model
  31. JQuery for Everyone: Print (Any) Web Part(s) Plugin
  32. JQuery for Everyone: Minimal AOP and Elegant Modularity
  33. JQuery for Everyone: Cookies and Plugins
  34. JQuery for Everyone: Live Events vs. AOP
  35. JQuery for Everyone: Live Preview Pane
  36. JQuery for Everyone: Pre-populate Form Fields
  37. JQuery for Everyone: Get XML List Data with OWSSVR.DLL (RPC)
  38. Use Firebug in IE
  39. JQuery for Everyone: Extending OWS API for Calculated Columns
  40. JQuery for Everyone: Accordion Left-nav with Cookies Speed Test
  41. JQuery for Everyone: Email a List of People with OWS
  42. JQuery for Everyone: Faster than Document.Ready
  43. jQuery for Everyone: Collapse or Prepopulate Form Fields
  44. jQuery for Everyone: Hourly Summary Web Part
  45. jQuery for Everyone: "Read More..." On a Blog Site
  46. jQuery for Everyone: Slick Speed Test
  47. jQuery for Everyone: The SharePoint Game Changer
  48. JQuery For Everyone: Live LoadTip
 

Please Join the Discussion

62 Responses to “JQuery for Everyone: Print (Any) Web Part”
  1. Jay says:

    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!!

  2. Joe says:

    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…

  3. 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

    • ld davis says:

      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

  4. Frank says:

    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

  5. Eric says:

    Can you link to the older code with the hard-coded reference to the specific WebPart to be printed?
    Thanks.

  6. Tamms says:

    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

Trackbacks

Check out what others are saying about this post...



Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!