Display Templates: Forcing Office Docs to Open in Browser

christineSharePoint, Templates & DownloadsLeave a Comment

When you’re using Content Search web parts to display documents, depending on your configuration, Office files will download instead of opening in the browser when clicked. Here’s a Javascript snippet you can add to your display template to prevent this behavior and force them to open in a browser:

var ext = linkURL.toString().slice(-3);
switch(ext)
{
     case 'ocx':
     case 'doc':
     linkURL = linkURL + "?Web=1";
     break;
     case 'lsx':
     case 'xls':
     linkURL = linkURL + "?Web=1";
     break;
     case 'ppt':
     case 'ptx':
     linkURL = linkURL + "?Web=1";
     break;
}
This gets pasted into the section with the rest of your Javascript. Remember to publish your display template after editing!

Leave a Reply

Your email address will not be published. Required fields are marked *