Using jQuery to Print a Web Part

Originally posted on: http://geekswithblogs.net/SoYouKnow/archive/2010/10/27/using-jquery-to-print-a-web-part.aspx

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:

PRINTAREA

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:

<script type="text/javascript" src="../scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="../scripts/PrintArea.js"></script>

 <SCRIPT type=text/javascript>
        function printPage(idOfWebPart){
            var printElement = document.getElementById(idOfWebPart);
            $(printElement).printArea({mode: "popup", popClose: false});
        };
</SCRIPT>

<input type=button value="Print Page" onclick="javascript:printPage('<ID of Web Part>');">
image 

Essentially, all we are doing is putting a button on the page and taking the ID of our DataViewWebPart and passing it to the printPage function when someone clicks the button. The PrintArea library prints out whatever HTML exists in the WebPart and even opens up what we are printing in a new window. Of course you can take this ugly button and make it an image, link, or whatever else your precious heart desires…

Ahhh.. but first we need to know the “id” of the element that contains our DataViewWebPart? How do we do that? Well.. there’s the right way, and then there’s the lazy way… I’m going to do the lazy way, but I’m sure there must be some better way.. again, feel free to enlighten us…  The lazy way is to view the page source and find our “id” in the page source (NOT the “WebPartID”).  This ID should not change, but if you use the same DVWP on another page, it most likely will NOT have the same id.  So.. to view the page’s source, right click on the page and click on… wait for it…. “View Source”… I know! right?

image

Now, when you are viewing the source it WON’T be pretty, but don’t panic… we can do a simple text search to find what we are looking for… I know from looking at my screen shot above, that the first entry in my DVWP is “Entry 1”, so why don’t we do a quick search. When I do that search it takes me to the following line in the source (I broke it up so that it is easier to read):

<td class="" valign="top">
<div WebPartID="45a689ea-04bf-4c51-90bb-824823298e81" HasPers="false" id="WebPartWPQ4" width="100%" allowDelete="false" style="" >
<table border="0" width="100%" cellpadding="2" cellspacing="0" xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">
<tr valign="top">
<th class="ms-vh" nowrap>Title</th>
<th class="ms-vh" nowrap>Hours</th>
<th class="ms-vh" nowrap>Entry Date</th>
<th class="ms-vh" nowrap>IssueID</th>
<th class="ms-vh" nowrap>Total Hours</th>&nbsp;
</tr>
<tr class="ms-alternating">
<td class="ms-vb">Entry 1</td>
<td class="ms-vb">4.00</td>
<td class="ms-vb">12/14/2009 12:00 AM</td>
<td class="ms-vb">1</td>
<td class="ms-vb"></td>
</tr>
.
.
.
                

As you see, the entire DVWP is inside the div: <div WebPartID=”45a689ea-04bf-4c51-90bb-824823298e81″ HasPers=”false” id=”WebPartWPQ4″ width=”100%” allowDelete=”false” style=”” >

What we need is the id… which in this instance is “WebPartWPQ4”, again NOT “WebpartID”.

So, in the html above, we would modify our button to look as follows:

<input type=button value="Print Page" onclick="javascript:printPage(‘WebPartMPQ4’);">

Now, that we are passing in the id of our DVWP, we should be good to go. So, when we click the “Print Page” button I should get a pop up window with a preview of what you will be print as well as a print dialog box:

image

There you go! Nothing much to it…

Scenario 2: Printing out Main Content of Page

So, what if you want to print all the content on the page except for the annoying search box and other artifacts that come across? Well, all you have to do for that is pass in the correct ID for the element that contains all the page content you want. Again, this can be done by viewing the source for the page. I can tell you that for a Out of the Box team site the ID appears to be “MSO_ContentTable

So, when I modify the button html above as follows we should be printing the main content area:

<input type=button value="Print Page" onclick="javascript:printPage(‘MSO_ContentTable’);">

Let’s click the button and see what happens:

image

As you can see, it only brings over the main content of the page (including the print button which you may find annoying).  I have not done a lot of research to determine how global the id “MSO_ContentTable” is.. I suspect it works for all non-publishing sites (but that is just a guess). If I wasn’t so lazy, I dig into it deeper, but hey, I gotta give you guys something to do.. right?

Well.. that’s all for now folks… Not rocket science, and maybe you have come up with a better solution, luckily there is a comments section below so you can enlighten all of us! 

Thanks once more for stopping by, hope you picked up a trick or two…

Families in Germany who are facing divers health problem, such persons can buy drugs from the Web without prescription. With the market flooded with divers web-sites selling sundry medicaments, purchasing medicines from th WEB is no longer a trouble for common man. Certain medications are used to treat infections caused by dental abscesses. Of course it isn’t all. If you’re concerned about erectile soundness problem, you probably know about Xenical and Hoodia. Probably every adult knows about Garcinia. (Read more PhentermineXenical). The symptoms of sexual heartiness problems in men turn on incredibility to have an erection sufficient for sexual functioning. Certain medications may add to sex drive difficulties, so its substantial to cooperate with your heartiness care professional so that the prescription can be tailored to your needs. Preparatory to taking Levitra or other medicament, speak to your pharmacist if you have any allergies. Talk to your health care purveyor for more details. Preparatory to ordering this remedy, tell your doc if you are allergic to anything.