<%@ Page Language="C#" Inherits="System.Web.UI.Page" %> <%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Import Namespace="Microsoft.SharePoint" %> <html> <head> <title>SharePoint List Print</title> <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/core.css" /> <script type="text/javascript" language="javascript" src="/_layouts/1033/init.js"></script> <script type="text/javascript" language="javascript" src="/_layouts/1033/core.js" defer></script> <script type="text/javascript" language="javascript" src="/_layouts/1033/ie55up.js"></script> <script type="text/javascript" language="javascript" src="/_layouts/1033/search.js" defer></script> </head> <body> <% string listId = ""; string referrer = ""; //get the list id (guid in a string format) from the query string listId = Page.Request.QueryString["list"]; //get the http referrer (for the back button\action) referrer = Page.Request.ServerVariables["http_referer"]; //make sure the list parameter was passed if (listId == null) { //if a referrer url exists (since the page may have been opened from a direct link, this is not always the case) redirect the user back if (referrer != null && referrer.Trim().Length != 0) { Page.Response.Write("<p>The list ID parameter ('list') is missing from the address.<br>Please go to the list you want to print and try again.</p>"); Page.Response.Write("<p><a href=\"" + referrer + "\" title=\"Go Back\">Click here to go back to the page you came from</p>"); } else { Page.Response.Write("<p>The list ID parameter ('list') is missing from the address.<br>Please go to the list you want to print and try again.</p>"); } } else { try { //load the web object for the site that the page is now in context of using (SPWeb web = SPControl.GetContextWeb(Context)) { //load the list that was passed in the 'list' querystring parameter to the page SPList list = web.Lists[new Guid(listId)]; //load the query of the default view. note - need to modify code in the future to enable multiple view printing SPQuery query = new SPQuery(list.DefaultView); //write the list to the page Page.Response.Write(list.RenderAsHtml(query)); //add the print script %> <script type="text/javascript" language="javascript"> window.print(); </script> <% } } catch (Exception ex) { Page.Response.Write("<p>There was an error loading the list information:<br />"); Page.Response.Write(ex.ToString()); Page.Response.Write("</p>"); } } %> </body> </html> <?xml version="1.0" encoding="utf-8" ?> <Feature Id="769826dd-9dd2-11db-96ca-005056c00008" Title="Print List" Description="This feature adds a print command in the Actions menu for Windows SharePoint Services lists." Version="1.0.0.0" Scope="Site" xmlns="http://schemas.microsoft.com/sharepoint/"> <ElementManifests> <ElementManifest Location="PrintList.xml" /> </ElementManifests> </Feature> <?xml version="1.0" encoding="utf-8" ?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <!-- Add the action to the List Toolbar Actions Menu Dropdown --> <CustomAction Id="SPSTIPS.PrintListActionsToolbar" RegistrationType="List" GroupId="ActionsMenu" Location="Microsoft.SharePoint.StandardMenu" Sequence="1000" Title="Print List"> <UrlAction Url="{SiteUrl}/_layouts/PrintList.aspx?list={ListId}"/> </CustomAction> </Elements>
Solution Architecture
The solution will be deployed as a wss feature, which allows us an easy way to add a menu item to the sharepoint menus. The feature will define that the item we want to add will be added to the actions menu of all lists, in a site collection. You can ofcourse change it so that it behaves differently and connects only to lists of a certain type if you so wish, or maybe move the menu item to a different place. I recommend reviewing the msdn article on the possible configurations.
The solution is based on 3 files:
Defines the feature, its scope and its title that you will see in the "site features" (or site collection features or farm features - depending on the scope)
PrintList.xml
Defines what action we want to add to what menu and what will happen when the user clicks the menu item. This is where you configure the text of the item, and the link to the page that will print the list. which just happens to be the last file:
PrintList.aspx
Contains the code that shows the list in a print-friendly view. This file should be deployed to the layouts folder and must be called with the site's context (more about that shortly).To the Code!
note - when I talk about the "12 hive" I am referring to the folder C:\Program Files\Common Files\Microsoft Shared\web server extensions\12
Create the page that prints a list:
Log on to the server, and open the template\layouts folder in the 12 hive.
Install the feature
In your editor of choice (notepad is fine) open the feature.xml file and paste into it the following:
In your editor of choice open the printlist.xml and paste the following:Almost done - install the feature and test it!
To install the feature, open command line (start>run>cmd) and navigate to the bin folder in the 12 hive.
Once there, run the following command to install the feature:
After you ran that command successfuly, you can activate the feature in the site collection either using the user interface (site actions> site settings> site collection features):
or you can (more easily since you are already in the command line) just run the following command, entering the site path:
stsadm -o activatefeature -name PrintListMenuAction -url [your site url here] -force
That's it! if you now go to any list in that site, you should have the print menu action.
0 comments:
Post a Comment