Tuesday, October 2, 2012

SharePoint 2013: PDF Preview in Search Results


Check out the whole SharePoint 2013 Solution Series
New Titles Added Weekly!

Introduction
One of the enhancements in SharePoint 2013 Enterprise Search results is the ability to preview documents using Office Web Apps Server. I am in the process of seeing what that looks like and will post an Office Web Apps server entry soon. UPDATE: Here is the post.

Anyway, the issue is that if you have Office Web Apps configured, the only documents that show previews are Microsoft Office documents (e.g. Word, Excel, etc.). The preview of PDF documents is not implemented or supported using Office Web Apps.

Therefore, I created my own PDF preview for Enterprise Search results by customizing the new search Display Templates. This works for local site search results too which I explain at the bottom of this post.

To understand overall PDF functionality and behaviors, read the whole story here.

Prerequisites
Here are some things you need in place before implementing this solution:

  • Enterprise Search Application configured and running
  • Crawled content that includes PDF documents
  • Enterprise Search Center site collection
  • SharePoint Designer 2013


Modifying the PDF Search Result Template
This is the main effort and is fairly easy. I found it better to modify the templates using SharePoint Designer 2013 although they are accessible through the SharePoint master page UI.

Step 1: Fire up SharePoint Designer 2013 and Open the Search Center Site

 
 
Step 2: Click on All Files from the left-hand navigation
If you attempt to get the files from the Master Pages, you will not see any items once you get to the Display Templates folders.


You will see the list of all files in the main window.

Step 3: Double-click on the _catalogs folder in the main window
This action displays the _catalogs structure under the left-hand navigation.


Expand the _catalogs folder, then the masterpage folder, and then the Display Templates folder.

Step 4: Click on the Search folder under Display Templates

 
The list of Search display templates is shown in the main window area.

Step 5: Right-click on Item_PDF_HoverPanel.html and select Edit File in Advanced Mode

 
 
Step 6: Paste the following code within the most inner <div>


    <object data="_#= ctx.CurrentItem.Path =#_" type="application/pdf" width="500px" height="630px" >
       <p>It appears you don't have a PDF plugin for this browser/device.
      You can <a href="_#= ctx.CurrentItem.Path =#_">click here to
       download the PDF file.</a></p>
    </object>


Note: You may find it better to use the following for width and height: width="100%" height="500px"


I actually replaced the Render Header and Render Body divs with the object code.

Step 7: Save the file
When saving the file, you may get a warning about breaking from the site definition. Click OK.
What happens behind the scenes is that the HTML changes are incorporated into the javascript version of the template (Item_PDF_HoverPanel.js).

Step 8: Test results
Perform a search from your search center that produces PDF document results. Hover over the PDF document to see the preview:


Currently the object tag uses hard-coded widths and heights. It is not using any responsive design styles. Me and my designer will be working on producing a more consistent respresentation for the PDF preview.

Local Search Implementation
There is still local site search results using the OSSSearchResults.aspx. You may implement the PDF Preview for these results as well but the modification is slightly different.

You open the site in SharePoint Designer 2013 and navigate to the Display Templates in the same fashion. However, for local (non Enterprise Search sites) there are no HTML templates - only the javascript files. You may simply copy the javascript that was created in your Enterprise Search Center site collection (when you modified the HTML) or paste the following code:

,'    <object data="', ctx.CurrentItem.Path ,'" type="application/pdf" width="280px" height="430px">'
,'       <p>It appears you don\'t have a PDF plugin for this browser/device.'
,'       No biggie... you can <a href="', ctx.CurrentItem.Path ,'">click here to'
,'       download the PDF file.</a></p>'
,'    </object>'

Save the file and preview the results by performing a local search in the SharePoint site.

It appears that the hover container in this case is being constrained in regards to its size. Therefore  I needed to use 280px for width and 430px for height. I will be working on making this more flexible.



To see what Office Web Apps server does for previewing of documents, see this post.
For Installation steps, see this post.

 

54 comments:

  1. Hello Steve,

    thanks for this nice write-up about the PDF preview in SharePoint 2013.

    I just wondered why there´s no preview and stumbled upon your blog.

    I´ll try out your solution.

    One question still remains:
    Do I have to have a separate Search Center Site Collection only for the Search Center? Currently I only got a Search Center Site in a Site Collection together with my other Content Sites and Webs.

    Greetings,
    Andy

    ReplyDelete
    Replies
    1. You should be fine with Search Center site. It should have the same structure. Let me know if you run into any problems.

      Delete
  2. where is "SharePoint Designer 2013 and Open the Search Center Site"

    ReplyDelete
  3. You need to download and install SharePoint Designer 2013. Once it is installed, launch the application and then open your Search Center site using File -> Open Site and entering the URL to your Search Center.

    ReplyDelete
    Replies
    1. Steve Mann Thanks
      PDF Now Working in Share Point.

      Delete
  4. Hello Steve,

    Thank you for posting this great article. I followed the steps and got the solution working apart from one small snag. It appears that for me the display area gets cut off on the right and as a result the scrollbar does not appear as shown in your example screenshot. I have tried making the width and height dimensions larger but to no avail. It seems to just ignore me and keep croping the view. I am sure I am doing something wrong... Perhaps you can offer some guidance on how I can correct this? I am using IE 8 on Windows 7.

    Thanks in advance for any advice you can share.

    Regards,
    Kevin

    ReplyDelete
    Replies
    1. Its probably ie8 causing the problem. SharePoint 2013 is really geared towards IE9 and above.

      Delete
  5. Hello Steve,

    Thanks for your reply. Unfortunately I seem to be experiencing the same problem after trying it in IE9 as well. Any additional thoughts on what I may be doing wrong?

    Thanks again.

    Regards,
    Kevin

    ReplyDelete
    Replies
    1. Are you using the search center or local search??

      Delete
    2. Meant to reply to the thread... I am using the search center. Thanks.

      Delete
    3. Try these for width and height:

      width="100%" height="500px"

      Delete
    4. Awesome! I looked at my staging instance and that's what I had in there - so I must have tweaked it previously. I'll update the post to mention this. Sorry for the trouble - I'm glad we figured it out though!!

      Delete
    5. No trouble at all. Great article and thanks for your help!

      Delete
  6. Hey Steve, great article.
    I'm trying to create a Design Template for search results on SharePoint Online. I don't have time or memory to stand up an on-prem environment.
    Problem is I can't see the HTML files to make a copy of the Display Template. Have you heard of this issue?
    Thanks.

    ReplyDelete
    Replies
    1. If you only see. Js files, follow the local search implementation at the bottom of this post.

      Delete
  7. Hi Steve!

    Great article but the preview is not very smart. i am not a good designer more an DB-Programmer. You wrote: ....Me and my designer will be working on producing a more consistent respresentation for the PDF preview....... PLease let me hear from you if you have a more beautiful solution. I urgently need this for my production SP2013 to get a better search experience and not to anger my Intranet Users:-( hope to hear from you with a adjusted solution.

    and: does your solution also work for integrated HTML Websites out of sharepoint?

    thx so much
    CHris
    thx su much

    ReplyDelete
    Replies
    1. The only modification so far was changing the width to 100% in the Search Center implementation. It may work in the local search too. That will at least adjust the preview to show the whole pdf page. I'm sure we'll investigate a more responsive design but it is not on the top of our list at the moment. I'll keep you posted with any updates.

      Delete
  8. Hi Steve,

    When I Expand the _catalogs folder, then the masterpage folder, I can't find the Display Templates folder.

    What I can find is only the forms folder.

    Any idea?
    Thanxs!

    Rgds,
    Mark

    ReplyDelete
    Replies
    1. Same here Steve, and yes I'm in the Search site. I created a fresh Enterprise Search site to experiment with your article.
      I see Forms (folder), _DeviceChannelMappings.aspx, minimal.master, oslo.master, oslo.previwe, seattle.master, seattle.preview, and v4.master. I looked at the properties of the folder, SPD settings, I'm stuck before I even get started.

      Delete
    2. You are clicking on All Files correct? You created a site collection with you being the site collection administrator, correct? Can you send me a screenshot? steve@stevethemanmann.com

      Delete
    3. I replicated the issue when I open a sub-site in SharePoint Designer. You need to go up to the site collection level.

      Delete
    4. Steve, I've looked at the root site collection as I'm trying to apply this to a sub site but the root site collection only shows JS files as well, any ideas? It's SharePoint Server 2013 I'm using. I noticed this has to be done on the search center, I don't have a specific search center setup is it not possible to do this on a SharePoint Subsite?

      Delete
    5. The root site collection needs publishing enabled. Modify the templates at the root.

      Delete
  9. Obvious question: is it a sharepoint 2013 site collection?
    I had a similar problem with a team site. I couldn't see the HTML page for the display templates just the JavaScript files. So I used a publishing template and then I saw them. Not sure if that's a bug or by design. Maybe you can't customize display templates in team site. I do like the display templates and JsLink. I used to do this crap using RenderingTemplates and customized ListForms in SP 2007.

    ReplyDelete
  10. This works great. Thanks! Any idea how to apply this to Word and Excel files bypassing the need to run an Office Web Apps server for preview? I had hoped that I could just change the "type="application/pdf" to " type="application/doc" in the Item_Word_Hoverpanel.html since they are just standard mime types but I get a blank document preview window. Maybe there is some other code that needs to be removed from that display template.

    ReplyDelete
    Replies
    1. I tried the same thing!! But I also experienced the same results. Office Web Apps Server in read only is free but it costs you another server. I may investigate again but I had no luck before.

      Delete
  11. Hi Steve,

    Were you able to get this working in a Document Library?

    Leo

    ReplyDelete
    Replies
    1. It is on my list. I'll look into it and let you know!

      -=Steve

      Delete
    2. Whoops. I did look at that before. For document libraries, SharePoint uses system-based javascript to display the hover panel. I didn't want to tinker with SharePoint .js files.

      Delete
    3. There is a filepreview.js file in the Templates folder of the 15 hive. I tried to tweak it for PDF but it didn't do anything. I added PDF as an extension and told it to use the [ImagePreview]. I then modified the image function to use the object tag if the filetype was PDF. That *stuff* didn't work. Maybe you can take a look at that. Remember to bounce IIS and maybe restart the AppFrabic service - so much caching going on in 2013 - you never know if your changes did anything.

      -=Steve

      Delete
    4. There is an Office Web Apps cumulative update that provides support for PDF via the Word App. I am trying to install but I am having issues. Sounds like this would provide rendering in a document library.

      Delete
  12. When I mouse over a PDF after making these changes, I get a Small pop-up box that says "Access Denied", with an OK button. When I press "OK" it shows the preview box, but not the PDF. I can click on any of these PDF's and open them though.

    ReplyDelete
    Replies
    1. It sounds like permissions but you do have access to open the document. The Search managed account has access since it crawled the content. Does your web app managed account have access to the documents?

      For Office Web Apps I needed to allow OAuth over Http but this should not be the case here since it's all within the farm.

      Here's the PowerShell if you want to try anyway:

      $config = (Get-SPSecurityTokenServiceConfig)
      $config.AllowOAuthOverHttp = $true
      $config.Update()


      Delete
    2. Great, I have been searching for this all over the place, going to try it and post whether it works.

      Delete
    3. This did not work for me...

      Delete
  13. Hi Steve,

    great post, it worked well for IE 10 and Firefox, not for IE9. It just shows an empty div :-(
    Any clue?

    Cheers,
    Marco

    ReplyDelete
    Replies
    1. I have it working in IE9. Are you missing an end tag or something (that maybe IE10 and Firefox compensates for)?

      Delete
  14. I am having same Issue as other Anonymous had described on May 17, 2013 at 6:27 PM. Difference for me is that it is only happening with documents which get crawled from file system. for e.g //shareddrive//yourfolder/document name. I get the same "Access Denied" pop up and when I click "ok" a empty frame opens up.
    I understand that there is no preview if document is not part of same site collection but why give a "Access Denied" pop up and blank frame.
    Also, please note that this is happening only in IE (I am using IE10) firefox and chrome it works fine.
    how can I get rid of this annoying error?

    Thanks

    ReplyDelete
    Replies
    1. I found that SP1 fixed many IE10 and IE11 issues but they just retracted it.

      Delete
  15. Hi Steve,

    PDF previews are working fine except for FBA users, do you have any workaround for that?

    ReplyDelete
    Replies
    1. Not at the moment. I don't have FBA setup to test either...sorry.

      Delete
  16. How can I show a preview of a site page in the hover pane?

    ReplyDelete
    Replies
    1. Any site or web page result should show that automatically.

      Delete
  17. Hi Steve,
    This was a great help, thanks so much. We are running Office Web Apps in SP2013 (on-premise). In our environment the thumbnail preview doesn't work in the OneNote hover panel either - would you know of a way to fix it also? Many thanks!

    ReplyDelete
    Replies
    1. I haven't worked with OneNote search results. I would first check the WOPI bindings to make sure OneNote is included.

      Delete
  18. This was awesome! Thank you.
    Could you possibly shed some light on why I get previews only for pages in the PAGES folder but not SitePages or any other Subfolder?

    ReplyDelete
    Replies
    1. Sorry for the late response - I was drowning in school work. It could be related to the search settings for the library or folder. It could also be related to the content types. I usually use the SharePoint 2013 Search Query Tool to troubleshoot search result issues. It is a free download -> https://sp2013searchtool.codeplex.com/

      Delete
  19. its possible to export version history to excel sheet?

    ReplyDelete
  20. Once again Steve an awesome solution. Here I am over three years after you wrote this, just now discovering it. Works great. One question I have is it possible to incorporate this into the hive in someway so that new sites create will already have this set up?

    ReplyDelete
    Replies
    1. Server-side is being phased out :)

      However, you could create a feature that added new templates into the directory when activated on the site.

      Delete
  21. One more thank you Steve for sharing this. Much appreciated.

    ReplyDelete