Tuesday, 22 August 2017

How to open files/documents in SharePoint document library is new tab

As you know documents in SharePoint document library (images, pdf files etc) always opens in the same page and it leaves you from the SharePoint context. You can not go back to SharePoint from that page unless you press the back button of your browser.

Here is a fix/workaround for this problem. Put below code to a js file and refer this file in your master page.

 try {
        //Below code is for opening document library files(pdf,jpg) in new tab
        // has to be on an interval for grouped doc libraries
        // where the actual links are loaded only once a group
        // is expanded
          function () {
              $("a[onclick*='return DispEx'][target!='_blank'][aria-label$='pdf File'],a[onclick*='return DispEx'][target!='_blank'][aria-label$='jpg File']")
                .attr("target", "_blank")
              // document type icons
                .click(function (e) {
                    window.open($(this).attr("documentUrl"), "_blank");
                    return false;
                .each(function () {
                      .replace(/["'{}]/g, "")
                    this.onclick = null;
    } catch (e) {

As you can see the above code works for all the document libraries and only for below file type:

  • PDF
  • jpg 
Since the ms-office file always opens in the office application or it downloads to the machine, we do not need to add them here. You can modify this code for png or any other types.

If this post was helpful to you, please consider visiting one or more advertisements on the page.Writing detailed post takes time, patience and advertising revenue helps to offset the effort.