1,558 articles and 11,138 comments as of Wednesday, May 26th, 2010

Monday, April 6, 2009

JQuery for Everyone: Print (Any) Web Part(s) Plugin

As I build up to the new, enhanced Preview Pane code, I want to highlight some of the more modular features it will have.

Find Something, Do Something With It

I improved the Print Any Web Part code and, once again, made it “plugin-style.” Now you can write your own links by using a selector, like $(”#WebPartWPQ1″), and add the .print() method. You can also add css instructions like so: print(css:”.ms-toolbar{display:none;}”).

Mash It Up!

If you mash this up with my other plugin, SharePoint Client-Side Object Model, you can find web parts no matter where they are on the page or which order they load in: $(”#”+wp.Tasks2.id).add(”#”+wp.Calendar.id).print().

<script type="text/javascript">
if(typeof jQuery=="undefined"){
	var jQPath="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/";
	document.write("<script src='",jQPath,"jquery.min.js' type='text/javascript'><\/script>");
}
</script>
<script type="text/javascript">
(function(){
	jQuery.fn.print = function(options){
		var o = $.extend({css:"",},options);
		var html = 	"<HTML>\n<HEAD>\n<style type='text/css'>\n"+
					options.css+"</style>"+
					$("head").html()+"\n</HEAD>\n<BODY>\n";
		this.each(function(i,e){
			html += "<DIV style='font-family:verdana,arial,helvetica,"+
					"sans-serif;font-size:8pt;margin:20px;'>\n"+
					$(e).html()+"</DIV>\n";
		});
			html += "</BODY>\n</HTML>";
		var printWP = window.open("","printWebPart");
		printWP.document.open();
		printWP.document.write(html);
		printWP.document.close();
		printWP.print();
		return this;
	};
})();
</script>

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

11 Responses to “JQuery for Everyone: Print (Any) Web Part(s) Plugin”
  1. Mike Slomanski says:

    I do not understand what I would do with this code or where to put it.?. Sorry if it is obvious. Looks useful.

  2. flume says:

    I agree with Mike. Would be nice with a simple example…

  3. Frank says:

    I added the code to my test site and it works. I have a few problems.
    1. How do I get the calendar to print on one page?
    2. How do I get it to work with other web parts? Here are some examples: (Hidden) Cross-Site Menu, (Hidden) LoadTip Live and other ‘great stuff’ I got from EndUsersSharePoint.
    I add it to the site and only the print code works.
    Hope you can help.

  4. AutoSponge says:

    @Frank,

    1. I’m guessing that the calendar will print to a single page if you can adjust your print settings. If not, you’re allowed to pass custom CSS instructions in the call to $.print(). See the example in this article.

    2. Check the versions of jQuery you’re using. If you’re mashing up web parts, make sure you use the same version everywhere.

    As for making specific code work together, I can only speak for my own creations and I never tested this with LoadTip Live.

  5. Charlie Epes says:

    Hi Paul:
    Would you give us some help on how to install and utilize this plug-in?

    Thanks-

    Charlie Epes

  6. AVIKA says:

    If I have 1000+ items, will it print page by page or you can all the pages?

  7. Sam says:

    Here is a simpler version of this code.

    It not jquery, but to be honest theres hardly a need for jquery on small functions like these..

    function print() {
    var a = window.open('', '', 'scrollbars=yes,width=850,height=650');
    a.document.open("text/html");
    a.document.write('');
    a.document.write(document.getElementById('print').innerHTML);
    a.document.write('');
    a.document.close();
    a.print();
    }

    • Thanks Sam. I’m in agreement that some of the things I’m seeing can be done with a simple javascript function as opposed to making a call to jQuery. Specifically, some of the interface enhancements available could just as easily be one with plain CSS in a CEWP. — Mark

  8. AutoSponge says:

    @Sam,

    Thanks for your suggestion. However, your script does not preserve the CSS/style used on the area being printed. You also neglected to mention that your version does not work in SharePoint because there is no element with id=print. I’ll also add, that part of this script’s utility is that it does not clog the global namespace with a generic name like yours, it extends jQuery instead–which followers of this article are already using for many other solutions.

    I’ll admit, I wrote this a long time ago, and probably would write better version today, but when I offered this, I tested it in multiple browsers in SharePoint–your code is just a copy-paste example from somewhere else.

Trackbacks

Check out what others are saying about this post...
  1. [...] friendly web content is another hurdle for application developers. I recently came across some useful jQuery for printing Sharepoint web parts. The idea was pure genius – wrap the bit of HTML you want [...]

  2. [...] two basic options for creating “plugins” (extensions of the jQuery library). Like my Print Any Web Parts code, you can create a jQuery method by using jQuery.prototype. or the pseudonym, [...]




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!