Add a printer friendly button to SharePoint publishing page

One of the request from my client is to allow user to print a page with only text content on it. This feature is quite common in many websites.

After reading Kathy Hughes “Microsoft SharePoint Designer 2010 Unleashed”, it’s pretty easy to do so in SharePoint 2010.

Scenario: I have 3 custom page layout for my site, home page layout, landing page layout and detail page layout. I only want to have a print button on pages that uses content page layout.

Here are the steps:

1. Create a new CSS file in the site collection’s Style Library and name it print.css. In this file, you can try hide all the components that you don’t want to see in the print view.

2. Open detail page layout. Add the following html inside the PlaceHolderMain tag.

< a id=”printer” onclick=”myPrinter(‘printer’)” href=”#” target=”_blank”><img src=”/Style Library/images/printer.png”></a>

3. On the same page, scroll to the top. Find the closing tag of PlaceHolderAdditionalPageHead. If you don’t have this tag, you need to add it first. Add the following JavaScript immediate before the closing tag. Of course you can add these code to a js file and add the link here instead.

<script type=”text/javascript”>
//if the url doesn’t contain printfriendly, use the normal style sheet
if (location.search.indexOf(‘printfriendly’) == -1){
document.write(‘<link rel=”stylesheet” type=”text/css” href=”/Style Library/css/gw_content_style.css”/>’);
}
//otherwise use the print style sheet and open the print window
else
{
document.write(‘<link rel=”stylesheet” type=”text/css” href=”/Style Library/css/gw_print.css”/>’);
window.print();
}

function myPrinter(id)
{
//if url doesn’t contain a question mark
if(location.search.indexOf(‘?’) ==-1)
{
var qrystr = document.getElementById(id).href;
//if  the url doesn’t contain a hash
if (qrystr.indexOf(‘#’) == -1)
{
//use the existing url.
//This means the button has been pressed before
//so the printfriendly parameter is already added.
document.getElementById(id).href = qrystr;
}
//otherwise the button hasn’t been pressed before
//so remove the # and add the printfriendly parameter
else
{
var newstr = qrystr.replace(‘#’, ‘?printfriendly=true’);
document.getElementById(id).href = newstr;
}
}
//this is used for pages like DisplayFullStory?ID=3 which has a parameter
else
{
var qrystr = document.getElementById(id).href;
if (qrystr.indexOf(‘#’) == -1)
{
document.getElementById(id).href = qrystr;
}
else
{
var newstr = qrystr.replace(‘#’, ‘&printfriendly=true’);
document.getElementById(id).href = newstr;
}
}
}
</script>

4. That’s it. When you test it, make sure you go to a page that uses the detail page layout.

 

Reference:

http://www.codingforums.com/archive/index.php/t-80517.html

Advertisements

Unexpected error when change page layouts

Problem:

Recently, our intranet has been renamed from gw to g10. With this change, some pages receives an unexpected error when changing the page layout.

Unexpected Error

I opened the site in SPD and tried “Detach from Page Layout”, threw the following error

 

 

question

After searching on the net, this article talked about the same problem and provided some power shell code to solve it. There were some drawbacks of this solution discussed in the comments section. So I didn’t take this approach. Another article really helped me. Thanks to the person who shared the problem and the solution.

Solution:

  1. Open the site which contains the page that had error in SPD.
  2. Go to All Files ->Pages->highlight the page that didn’t work.
  3. From the top ribbon, select Export File. Save to a location where you can find later on.
  4. Right click on the exported file and open in notepad.
  5. Ctrl + F and search for “_catalogs/masterpage/DefaultLayout.aspx”. You should only find one result and notice that link pointed to the wrong domain.
  6. Change the domain to your current correct domain and Save.
  7. Copy the file back to SPD, overwrite the existing one.
  8. Go back to the browser and change a page layout. It works!

 

Page Layout dropdown is not showing on edit mode

When I edit a page on a publishing site, I don’t see the dropdown list when I click on the Page Layout button from the ribbon. After searching on Google, I found two possibilities:

  • Don’t have enough permission
  •  SharePoint ribbon tries to load unexisting content and thus errors and cannot show the dropdown.

I checked my permission and got full control. Also noticed that for users who don’t have enough permission will simply have the Page Layout button greyed out. But for me, the button was still clickable, just no dropdown.

Found out that when the site was copied across from another site using 3rd party software, my customized content type didn’t get copied across. Therefore, its associated page layout couldn’t find the content type.

So I created the content type in SharePoint Designer:

  1. In the Navigation Pane, click “Content Types”
  2. From the ribbon, in the New section, choose Content Type
  3. Enter Name, Description.
  4. Parent content type from: Publishing Content Types
  5. Parent content type: Page
  6. Create a new group, e.g. My Custom Group
  7. Click OK, and you are finished with creating a content type in SPD.
  8. Go to the browser, Site Actions->Site Settings->Galleries->Master pages and page layouts->Select the page layout and Edit Properties
  9. Content Type: Page Layout
  10. Associated Content Type: Content Type Group: choose the group you created in step 6 Content Type Name: choose the content type you created in step 3
  11. Save and done!

Free SP2010 master page template

Last couple of weeks, I’ve been working on branding two of our internal SP2010 websites. To quickly get something up and running, I decided to use some ready built master pages and page layout. Found this article with a free SP2010 master page template. It was very helpful, however, initially I didn’t finish reading all the comments and so didn’t realise it doesn’t work in IE7.

Here is some fixes I added to make it display in IE7 properly:
[Coming…]