logo

SharePoint: Print Friendly Button on a List Item

So, I needed to add a printer-friendly option to a SharePoint List Item. After a bit of Googling I found that most suggestions were based upon using print-based StyleSheets, whereas I wanted something a bit more solid. What I ended up doing was adding a completely new page that gave me full control of what appeared when printed. Here's how I did it.

The Print Popup

Out of the box SharePoint gives you a standard toolbar on each List Item, like the one below, which also has the custom button that I'm going to show you how to add later on.

 image

When clicked this new button opens a popup with a page that's ready for printing, like below:

image

Note that the two buttons you can see on the popup are hidden from printing using some CSS on the page.

Adding The Button

To add this button to the toolbar of the List Item in question I first created a new Feature in my WSPBuilder project in Visual Studio.

In the resulting elements.xml file I then added a <CustomAction> child to the <Elements> tag, which looked like this:

<CustomAction
        Id="ACME.Actions.PrintStep"
        Location="DisplayFormToolbar"
        Title="Print Step"
        RegistrationType="ContentType"
        RegistrationId="0x0100450FD7A78A6342D69B22CE97"
        ImageUrl="~site/_layouts/ACME/images/printer.png">
 <UrlAction Url="javascript:window.open('{SiteUrl}/_layouts/ACME/print.aspx?List={ListId}&amp;ID={ItemId}','PrintStepForm','height=600,width=575');return false;"/>
</CustomAction>

You should be able to work out what's going on here. When the project is built and deployed and the feature is enabled through the Site Settings then you get a new button on the "read mode" tool bar for list items based on the specified content type (whose ID is listed in the RegistrationId attribute).

Adding the Print Form

You'll notice quite a few references to the "_layouts" directory in the XML code above and may be wondering what it is? Well, I'm no expert and not sure exactly what it is, but, as I see it, it's somewhere to put custom ASP.NET pages, images etc.

You can see the required folder structure in this WSPBuilder project below. To make them available in the actual "_layouts" directory for your site you just right-click the "ACME" parent folder in Visual Studio and choose "Copy to 12 Hive".

image

It's the print.aspx page that we're going to use to display our print-friendly page in a popup. Notice in the button's XML that the URL used to open this page passes to it the ID of List and the List Item, so we can then get a handle on them in the "onload" event of the ASPX page.

Here's the full code for the Print.aspx page:

<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%> 

<%@ Page Language="C#" %> 
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" 
        Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 

<script runat="server">
    protected override void OnLoad(EventArgs e)
    {
        try
        {
            SPWeb oWeb = SPControl.GetContextWeb(HttpContext.Current);
            
            SPList oList = oWeb.Lists[new Guid(Context.Request["List"])];
            SPListItem oItem = oList.GetItemById(int.Parse(Context.Request["ID"]));

            header.Text = oItem.Title;
            
            if (oItem["Content"]!=null)
            {
                procedures.Text = oItem["Content"].ToString();
            }
            
        }
        catch (Exception ex)
        {
            Context.Response.Write("Error: " + ex.StackTrace);
        }
    }
</script>
<html>
<head>
<title>Print Friendly Page</title>
<style type="text/css" media="print">
.button{
    display:none
}
</style>
</head>
<body>

<p class="button">
 <input type="button" value="Print..." onclick="window.print()" /> 
 <input type="button" value="Close" onclick="window.close()" />
</p>

<h1><asp:Literal ID="header" runat="server" /></h1>

<asp:Literal ID="procedures" runat="server" />

</body>
</html>

As you can see the OnLoad page event fills the to Literal elements with data from the List item, which it got to using the URL parameters passed.

Summary

This approach probably isn't as simple as adding a special print stylesheet to the item itself, but this is sometimes my preferred approach. Especially when dealing with HTML so overly verbose as SharePoint's, where the CSS to hide everything but the relevant content must be a nightmare to compile and maintain.

Either way it's a nice simple example of adding custom toolbar buttons and ASPX pages to the _layouts directory.

Comments

    • avatar
    • Diana
    • Wed 11 Aug 2010 03:10 PM

    what has this in common with Lotus und Planetlotus?

      • avatar
      • Jake Howlett
      • Wed 11 Aug 2010 04:04 PM

      I'm guessing that's a joke?

    • avatar
    • benny
    • Wed 11 Aug 2010 08:39 PM

    Good feature, Jake! Thanks for sharing:)

    • avatar
    • Fabrice
    • Thu 12 Aug 2010 02:06 AM

    This Sharepoint seems so complicated.

    What a shame Lotus losed the battle.

    In France too, more and more customers give up Lotus. :-(

      • avatar
      • WOTAN
      • Thu 12 Apr 2012 04:04 PM

      Just more proof that the world has lost its mind. More and more I am believing in Satan - this IS the work of something EVIL. What it really amounts to is EVIL, in that money is the root of all EVIL - managers want to reduce their budgets, so they buy the cheapest thing they can find, and we get what they pay for.

    • avatar
    • Christain
    • Thu 12 Aug 2010 05:53 AM

    that's a marketingsproblem and note a productproblem.

    they can sell (worse product) better than IBM.

    nobody knows quickr or quickplace or ...

    • avatar
    • Carl
    • Thu 2 Dec 2010 09:32 AM

    Hi,

    I'm trying to do this in sharepoint 2010 and not really getting it to work. How would you solve printing items in a list in sharepoint 2010?

    //Carl

  1. For those who are not developers and don't want to hassle out with Visual Studio - you might want to try Infowise Smart Print Pro which is a list printing solution for SharePoint 2007, 2010 and WSS

    Check it out here:

    http://www.infowisesolutions.com/product.aspx?id=SmartPrintPro

    Ethan Bach

    Infowise

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Wed 11 Aug 2010

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

More Content