Thursday, July 15, 2010

Easily print SharePoint lists using your own template !

Recently I have really wanted to easily print some SharePoint lists, but I always had to open them in Excel, format the columns and then print...this takes time. I looked at  Ishai Sagi's solution, but it didn't really do what I wanted...I really wanted the ability to print directly from the browser using a variety of templates. This also coincided with Brent starting a new CodePlex project to easily add functionality to a SharePoint site without having to use features or solutions. Essentially the project provides functionality for people where no binaries or layouts and can be deployed, and so can be can be used by people on shared hosting. I had agreed to work with him on the project and so thought I could combine the two!

The plan

The idea I had was to add some menu items to the standard WSS menus using javascript and to do all formatting client side. Obviously I chose XML & XSLT to do this, mainly because the SharePoint web services return XML, but also because it makes creating new templates so easy.

How it works

The javascript adds menu items to both the 'Actions' menu of a list view and also the to drop-down menu of the list items within the view...how this is done I will leave for another post! In response to choosing print a new window is shown, the list is retrieved using web services and then transformed using XSLT. This is shown in a DHTML dialog from which you can print it, print preview it or select a different template. Templates are defined in a document library using XSLT and you can add as many as you like.

What does it look like

Here are some screen shots from my blog admin site...

image image image

In the last screenshot you see the print dialog displaying what is actually going to be printed shown. From here you can choose to print, print preview or change the template...

image

Samples

You can download some sample prints ...

Item print sample (.xps) 

List print sample (.xps)

These samples show the pretty basic templates which are included. However they are generated using XSLT and so you can easily create your own by uploading them to the document library.

Installation

The functionality is provided as a list template. You need to download the .stp from the CodePlex project site and then upload it as a new List Template...

image

Once uploaded you can create a new document library using the template...

image

The template contains all the files required for the feature and once created you should see the templates...

image

You can now add the following lines to your default.master for your site just above the </body>...

<link rel="stylesheet" href="/PrintTemplates/printwindow.css"/>
<script language="javascript" src="/PrintTemplates/printwindow.js"></script>
<script language="javascript" src="/PrintTemplates/PrintFunctions.js"></script>
<script>_tk_enablePrintFunctions("/", "PrintTemplates")</script>   

Change the 'PrintTemplates' to be the same as the name of your document library.

You should now be able to navigate to any list and print the items.

Problems

Currently the two main problems are that I have only tested it with IE7 (no VM with IE6) and I have to use an ActiveX browser object for the print preview. Therefore in order for the print preview to work your site needs to be in the 'Local Intranet'  zone, if not you will see the following error...F5 will return you to the list.

image

Try it out

Brent has created a demonstration of the functionality Sharepoint Templates site.

Visit the SharePoint templates project on CodePlex for more information

You can download the ZIP containing the required files here. These can be placed in any document library or on the file system...as long as you can reference them in the src= of your SCRIPT tag.

1 comment:

  1. Hi Srikanth, I saw your solution and it seems to be exactly what I need to accomplish for my company's sharepoint site. Only thing is the images and the files are not available anymore.

    ReplyDelete