Sunday, May 19, 2013

Print SharePoint Listview Web Part using JavaScript

Requirement is to Print a SharePoint List view web part with JavaScript.

Solution: Found this code somewhere on the Technet Forums: Place this code in a CEWP, This will add a "Print" button, on clicking the button, Script fetches the list view web part, place it in a new window and sends to Print.

<INPUT type="button" onclick="javascript:void(printwebpart('WebPartWPQ2'))" value="Print"/>

<script type="text/javascript">
function printwebpart(webpartid)
{
var WebPartElementID = webpartid;
var bolWebPartFound = false;

if (document.getElementById != null)
   {
  //Create html to print in new window
  var PrintingHTML = '<html>\n<head>\n';

 //Take data from Head Tag

  if (document.getElementsByTagName != null)
  {
  var HeadData= document.getElementsByTagName("HEAD");

  if (HeadData.length > 0)
  PrintingHTML += HeadData[0].innerHTML;
  }

  PrintingHTML += '\n</HEAD>\n<BODY>\n';
 var WebPartData = document.getElementById(WebPartElementID);

 if (WebPartData != null)
 {
  PrintingHTML += WebPartData.innerHTML;
  bolWebPartFound = true;
 }
 else
 {
   bolWebPartFound = false; alert ('Cannot Find Web Part'); 
 } 
   } 
  PrintingHTML += '\n</BODY>\n</HTML>';
 
  //Open new window to print

  if (bolWebPartFound)
  {
   var PrintingWindow = window.open("","PrintWebPart", "toolbar,width=800,height=600,scrollbars,resizable,menubar");
   PrintingWindow.document.open();
   PrintingWindow.document.write(PrintingHTML);
   PrintingWindow.document.close();
   PrintingWindow.focus();

   // Open Print Window
   PrintingWindow.print();
   PrintingWindow.close();
  }

}

</script>  
Output:



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

2 comments :

  1. Nice article, but for SPS 2013 it keep closing pop up window automatically.

    ReplyDelete
  2. Thanks for the nice post.

    Mahmood Algoul - Comment the below line and see how it works in IE. I found that this one won't work in Chrome.
    //PrintingWindow.close();

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...