0

The Problem:

I am trying to find a way to open up SharePoint Document Library documents in a new tab. This seems like it should be a setting option in SharePoint; however, if it is, I'm unable to find it.

  • Is there a setting I'm over looking?
  • Does it involve custom code?

I have tried some custom code I found on the web as well as here on stackexchange, but I haven't been able to get them to work.

If any one could shed some light on how exactly to implement code for this, it would be appreciated.

  • Just wanted to add a big Thanks. Been looking for this answer for a while. Jess – jslist Jun 12 '17 at 19:28
  • M.Qassas this works great, but is there a possibility to specify library. I have a page with multiple doclib web parts and now every web part is opening document in new tab. And i have doc lib that has documents that needs to open in client application. Those documents are going to download when i click them, if script is on page. Thanks – Oremmus Jul 14 '17 at 10:52
6

Follow the mentioned steps below :

  • Go to your document library page > edit page.

  • Add script editor web part

enter image description here

Add this code that tested and from my side (Code Ref : Opening a document in a SharePoint 2013 document library in a new tab (using code) )

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

<script>

$(document).ready(
  function ()
  {
    // has to be on an interval for grouped doc libraries
    // where the actual links are loaded only once a group
    // is expanded
    setInterval(
      function ()
      {
        $("a[onclick*='return DispEx'][target!='_blank']")
          .attr("target", "_blank")
          .removeAttr("onclick");

        // document type icons
        $("td.ms-vb-icon>img[onclick]:not([documentUrl])")
          .click(function (e)
          {
            window.open($(this).attr("documentUrl"), "_blank");
            e.stopPropagation();
            e.preventDefault();
            return false;
          })
          .each(function ()
          {
            $(this).attr(
            "documentUrl",
            $.trim(String($(this).attr("onclick"))
              .split("=")[1]
              .replace(/["'{}]/g, "")
              .split(";")[0])
            );
            this.onclick = null;
          });
      },
      500
    );
  }
);

</script>
  • Again, Go to your document library > from the above ribbon > library setting > Advanced setting . check open in browser .

enter image description here

  • Awesome, it works just like it is suppose to! Thanks so much – JacobCaldwell Nov 10 '16 at 19:55
  • Glad to hear it helped you @AmayKulkarni , please don't forget to upvote the helpful answer, Thanks! – Mohamed El-Qassas MVP Oct 15 '18 at 12:04
  • It works great in Chrome but not in Internet Explorer (I have IE11). – Soul Eeater Mar 4 '19 at 19:22
  • Excellent thanks, works like a charm. I have a master page in place, can I add this script at the master page to apply it to all libraries in the site collection – mdevm Jul 5 '19 at 18:10
  • @mdevm, it should work, but you will get JS errors in other pages! Please don't forget to upvote that answer if it helped you! – Mohamed El-Qassas MVP Jul 5 '19 at 18:22
0

Is this needed for all libraries in your environment or just a select few?

Document libraries have a setting to determine how office documents open. Library Settings > Advanced Settings > Opening Documents in the Browser. (browser/client/server default)

If you just need to do this for a few special documents here are the other areas of SharePoint where I know you can specify that the link open in a new window.

  1. site Navigation links have the 'open in window' check box so that is one option if you want to manually add links to each document in a top or left navigation.
  2. you can add a link to do this on a content page such as a wiki page or content editor web part. type the text then insert a link, you will then have the option in the ribbon under the link settings to open it in a new window.

Not the answer you're looking for? Browse other questions tagged or ask your own question.