四步实现sharepoint的打印功能

来源:互联网 发布:excel 地图指引数据 编辑:程序博客网 时间:2024/05/10 19:15
Printing Web Parts in SharePoint
Imagine this: You’ve just taken the time and effort to modify the look and feel of the content of a web part.  Perhaps you even created a sophisticated display of data using CorasWorks Custom Display Properties or the List Viewer web part.  It works, and, brings back the data you want – looks great on the screen.  Now, you want to have a nice printout of the data viewed in the web part.  What you need is a Print Button?  This article will tell you how to do this in 4 easy steps.

 

By following the 4 steps below, using native SharePoint web parts, and the code provided, you will be able to add a Print Button to a page that will print the contents of any web part.  You could even have multiple Print Buttons for different web parts or a button to print the contents of many web parts – but first, let get you one Print Button.

1. Start with a web part

You start by having a web part with a display of information, such as a list.  If you don’t have one already you can start by creating a SharePoint Event List, and then, adding a Calendar web part (remember to choose the Calendar View).  Or, for CorasWorks users, use any Roll-Up view of data.

2. Add the Print Button

You will be adding a Print Button to the page, by putting the JavaScript below into a Content Editor Web Part.

  1. Add a Content Editor Web Part to the page with the web part you want to print.
  2. Open its properties and click the Source Code button to add the JavaScript code.
  3. Copy the following code directly into the Text Builder box. This code will create a “Print Web Part” button that when clicked, will execute the print action.

<center><input type="button" OnClick="javascript:void(PrintWebPart())" value="Print Web Part"></center>

<script language="JavaScript">
//Controls which Web Part or zone to print
var WebPartElementID = "WebPartWPQ6";

//Function to print Web Part
function PrintWebPart()
{
 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);
  // Open Print Window
  PrintingWindow.print();
 }
}
</script>

3. Connect the Print Button to the Web Part

To do this we need the <DIV> tag ID for the Web Part we want to print, then, we modify the ID in the JavaScript of the Print Button.

To find out the ID of the Web Part we want to print:

  1. Using your browser, right mouse click on the page where the Web Part is installed and choose “View Source”. This will open a view of your page in HTML within Notepad.
  2. Press CTRL-F, to initiate a Find. Enter the Title of your Web Part. You may need to execute a find a couple of times as your navigation may show “Events” as well.
  3. Once you have located the HTML for your Web Part for example; “<td accesskey="W" tabindex="0" title="Events" id="WebPartTitleWPQ6" style="width:100%;">”, look later in the HTML for a <DIV> tag which matches the Web Part Queue Number, in this case WPQ6. In our case it looks like “<div WebPartID="d44df3e3-0bbc-4151-9d04-22982bd294bc" HasPers="false" id="WebPartWPQ6"…”.
  4. The “WebPartWPQ6” would be the ID we want.  The key part of the ID is to know if it is 1, 2, 3, etc.  Here it is 6.

To modify the Print Button JavaScript:

  1. Modify the Content Editor Web Part which contains the JavaScript you pasted earlier and go into the Source View.
  2. Look for the line “//Controls which Web Part or zone to print”, the line underneath controls the ID near the top.
  3. Replace the “WebPartWPQ6” with the ID you copied from the source of the page and click “OK”.
  4. Save the changes and click OK.

4. Print the Web Part Content

We are now ready to test what you have labored to create. Once the page has been refreshed inside of Internet Explorer, click on the “Print Web Part” button. You should see a new Window with the content of your web part.

  • If your pop-up blocker IS NOT turned on, you will get the Print Dialogue.
  • If it is turned on, then you can go to File Print or Print Preview.
  • Now, print the content.

Notes and Tips

  • You may notice that a JavaScript error occurs during the printing - this can be ignored. Since you are not within a Web Part Page all of the functions necessary for menus, or trees, etc… will not have the necessary JavaScript they need to function.
  • You may want to change the button text.  Go to the Content Editor Web Part, find the text “Print Web Part”, and change it to what you want.
  • You can also put multiple buttons on a page, each connected to print one web part.  Think of it as a centralized report list.
  • If you are ready to really spice things, up you can modify the JavaScript code as necessary and use CSS to alter the Page Layout when printing.
  • You could also use FrontPage to place your <DIV> tags around a Web Part Zone so that the Zone and all of its containing Web Parts will print.

We hope you enjoyed the article and found the information contained to be helpful. Enjoy and Happy Architecting.

 

原文:http://www.imakenews.com/mernstmann/e_article000435389.cfm

 
原创粉丝点击