Wednesday, April 17, 2013

How to enable page previews in SharePoint 2013 for content not on the Search Center host domain

The solution

  • Edit Item_WebPage_HoverPanel.html
  • Add <WebPartPages:AllowFraming runat="server" /> to your master page
Beware: This solution opens up for click-jacking, but should not be a real threat on a intranet scenario.

The journey

Ok, you start off your new SharePoint 2013 and try to be smart regarding web applications, site collections and domain names.
The full 2013 solution has one intranet and collaboration part and one part for a QMS system. Early on you agree to use the following domain structure which should make sense to the end-user.
Everyone is happy and after a month or so the issue of search comes up. To align with the existing structure you go with:

You kick off a full crawl, and start looking at the results, in particular you want those cool new previews.
No preview :(

image

Ok… don’t give up. This should be easy enough to fix. You locate Item_WebPage_HoverPanel.html and find the following line

if(!Srch.U.w(ctx.CurrentItem.csr_Path) && Srch.U.isSameHost(ctx.CurrentItem.csr_Path, Srch.U.getHostName())) {

What this does is to check if the item you are viewing is on the same domain as the search page. Of course not! We just decided to use domain names as content separators in the beginning. Ok… hmmmm.. let’s just replace the line with

if(true)

Back to the search page and reload to refresh the display template.

image

The text is pretty small, but it says: “This content cannot be displayed in a frame”. Now you start to get frustrated and fire up some internet searches. You stumble upon “IFraming SharePoint-hosted pages in apps” and find out that SharePoint 2013 adds X-Frame-Options HTTP headers to deny ClickJacking.

But, there is fortunately a fix. By adding the following control to your pages it will not send out the X-Frame-Options header.

<webpartpages:allowframing runat="server" />

As we have custom master pages all around we added the control just below the Register directives in our master pages.

Again back to the search page and reload with a working result!

image