Using jQuery to Print a Web Part

Someone drank the jQuery Kool-Aid…

Here’s a super quick and simple blog post that you may or may not find useful… as always I offer a money back guarantee…

Let’s say you want to print out your SharePoint page but you don’t want the Search Box or anything outside the main content area to print, or maybe you just want one specific web part on the page to print; how would you do that? I spent about 5 minutes Googling it on Bing (I heard that phrase at SPTechCon last week… really wish I could remember who told me, but I can’t take credit for making it up) and found a simple jQuery library which fit my needs perfectly.

There is a jQuery library called “PrintArea”, it can be found at:

Using this library and only a few lines of code you can add a print button or link on any page and just print out the section of the page you want to.  First thing you want to do is download the PrintArea.js file and upload it to a document library or deploy it to your file system like I have described in previous posts. Also, make sure you have access to the jQuery library as well. Again, I will just place these in a “scripts” document library for this example. Once you have that done, we are ready to go.

Scenario 1: Printing out a specific Web Part

So, you went to all that trouble to create that pretty DVWP and you would like to give the user the ability to easily print out just that DVWP. What do you do? 

Let’s say you have a DataViewWebPart like the Time List below and you want to print out JUST the time list. 

image

First thing you need to do is add a Content Editor Web Part (CEWP) to the page.

image

You then need to place the following script into the CEWP:

This article is part of the GWB Archives. Original Author: Mark Rackley

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.