SharePoint display templates – Link to “open in browser” with Office 365 Online

Total visits : 1378

In Office 365 SharePoint, you probably want to take advantage of Office 365 on-line to to open and edit Office documents (Word, Excel, Power Point, One Note, PDF, etc…) in the browser.

All the document libraries Microsoft implemented, if you are using search results display templates (like Two Lines template), always the file download while you click the links.

How to implement while open the office files, required to update the two_lines.html file. I am here using Two Lines template to explain.

How to bring the URL to open the file file using office 365 on-line

Any document will open in browser if you form the URL like this

Syntax – _layouts/15/WopiFrame.aspx?sourcedoc=<Doc URL>&action=default

Example: https://jenkins.sharepoint.com/sites/dev/teamsite/_layouts/15/WopiFrame.aspx?sourcedoc=https://jenkins.sharepoint.com/sites/dev/teamsite/Shared Documents/folder1/sampledocument.docx&action=default

if your file in subsite, you should give the subsite url with WopiFrame.aspx file, otherwise it will throw internal error. But in display template it will give the full file URL, the result comes from different site collection and sub sites. so that here we need relativeURL, i.e.. without library name, folder name and file name in the URL. to get the relative URL you need to call a async call using JSON.

Steps to follow to implement

  1. Open the “Item_TwoLines.html” file – _catalogs/masterpage/Display Templates/Content Web Parts/Item_TwoLines.html
  2. Edit File in Advanced Mode
  3. Add below code next to this line var line2Id = encodedId + “line2”;

var srcURL = ctx.CurrentItem.Path; //Get the current file full URL
var fileextension = ctx.CurrentItem.FileExtension; //Get the current file extention
if(fileextension == “aspx”)fileextension=null; //no need to open aspx files in office online
if(fileextension == “pdf”)fileextension=null; //no need to open aspx files in office online
srcURL = srcURL.substring(0, srcURL.lastIndexOf(‘/’)) + “/_api/contextinfo”; //build a JSON url to get the web URL
var src;
getresult(srcURL); //get the web url using JSON
if (fileextension != null)
{

_#–>
<div class=”cbs-Item” style=”padding-bottom:6px !important” id=”_#= containerId =#_” data-displaytemplate=”Item2Lines”>
<a class=”cbs-ItemLink” title=”_#= $htmlEncode(line1.defaultValueRenderer(line1)) =#_” id=”_#= pictureLinkId =#_” href=”_#= src =#_/_layouts/15/WopiFrame.aspx?sourcedoc=_#= linkURL =#_&action=default”> //added the href link to open the file using office 365 on line
<img class=”cbs-Thumbnail” src=”_#= $urlHtmlEncodeString(iconURL) =#_” alt=”_#= $htmlEncode(line1.defaultValueRenderer(line1)) =#_” id=”_#= pictureId =#_” />
</a>
<div class=”cbs-Detail” id=”_#= dataContainerId =#_”>
<a class=”cbs-Line1Link ms-noWrap ms-displayBlock” href=“_#= src =#_/_layouts/15/WopiFrame.aspx?sourcedoc=_#= linkURL =#_&action=default” title=”_#=

//update the href link to open the file using office 365 on-line

$htmlEncode(line1.defaultValueRenderer(line1)) =#_” id=”_#= line1LinkId =#_”>_#= line1 =#_</a>
<!–#_
if(!line2.isEmpty)
{
_#–>
<div class=”cbs-Line2 ms-noWrap” title=”_#= $htmlEncode(line2.defaultValueRenderer(line2)) =#_” id=”_#= line2Id =#_”>_#= line2 =#_</div>
<!–#_
}
_#–>
</div>
</div>
<!–#_
}
else
{
_#–>
<div class=”cbs-Item” style=”padding-bottom:6px !important” id=”_#= containerId =#_” data-displaytemplate=”Item2Lines”>
<a class=”cbs-ItemLink” title=”_#= $htmlEncode(line1.defaultValueRenderer(line1)) =#_” id=”_#= pictureLinkId =#_”>
<img class=”cbs-Thumbnail” src=”_#= $urlHtmlEncodeString(iconURL) =#_” alt=”_#= $htmlEncode(line1.defaultValueRenderer(line1)) =#_” id=”_#= pictureId =#_” />
</a>
<div class=”cbs-Detail” id=”_#= dataContainerId =#_”>
<a class=”cbs-Line1Link ms-noWrap ms-displayBlock” href=”_#= linkURL =#_” title=”_#= $htmlEncode(line1.defaultValueRenderer(line1)) =#_” id=”_#= line1LinkId =#_”>_#= line1 =#_</a>
<!–#_
if(!line2.isEmpty)
{
_#–>
<div class=”cbs-Line2 ms-noWrap” title=”_#= $htmlEncode(line2.defaultValueRenderer(line2)) =#_” id=”_#= line2Id =#_”>_#= line2 =#_</div>
<!–#_
}
_#–>
</div>
</div>
<!–#_
}
function getresult(srcurl) //added new function to get the web url
{
$.ajax({url: srcurl, async: false, type: “POST”, contentType: “application/x-www-url-encoded”,dataType: “json”, headers: {“Accept”: “application/json; odata=verbose”},success: function (data) {
if (data.d) {
src = data.d.GetContextWebInformation.WebFullUrl;
}
}});
} //Here I used sync call, Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user’s experience. Please use callback async call.

_#–>

Good Luck.