0

How do i tell Sharepoint to always open documents(mostly .pdf and .docx/.doc)in a new tab when they are saved in a document library?

This has been disgussed in another questinon before.

I tired to add the code just before the "body" tag ends like this:

<script src="ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js"> 
$(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>

But nothing changes. Does anyone know what the problem is or a different solution?

  • SharePoint 2013 or 2010? – Atish Dipongkor - MVP Oct 22 '15 at 10:39
  • 2013 Foundation.. Sorry.. Forgot to point that out. – Tom Oct 22 '15 at 10:43
  • Do you get any JavaScript error? Are you sure JQuery is correctly loaded? Do you see in the dev tools (F12) if the .click function is called? – Evariste Oct 22 '15 at 10:52
  • There aren't any JavaScript errors. Actually.. Im not cause i've never worked with JQuery before. And nope... The .click function isn't called... – Tom Oct 22 '15 at 11:06
  • If it is possible, then follow my answer. It's better than you are trying to. – Atish Dipongkor - MVP Oct 22 '15 at 11:07
0

If I got you requirement correctly, then try to accomplish it using JSLink rather than using Content Editor Web Part. Following code snippet helped me to open document in a new tab whenever I click on the file name.

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {

  function getBaseHtml(ctx) {
    return SPClientTemplates["_defaultTemplates"].Fields.default.all.all[ctx.CurrentFieldSchema.FieldType][ctx.BaseViewID](ctx);
  }

  function init() {

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides({

      // OnPreRender: function(ctx) { },

      Templates: {

        //   View: function(ctx) { return "LinkFilename"; },
      //     Header: function(ctx) { return ""; },
      //     Body: function(ctx) { return ""; },
      //     Group: function(ctx) { return ""; },
      //     Item: function(ctx) { return ""; },
           Fields: {
               LinkFilename: {
                   View: function(ctx) {
                     var url = ctx.CurrentItem["FileUrl"];
                     return String.format('<a href="{0}" target="_blank">{1}</a>', url, ctx.CurrentItem["FileLeafRef"].substr(0, ctx.CurrentItem["FileLeafRef"].lastIndexOf('.'))); },
                   //EditForm: function(ctx) { return ""; },
                   //DisplayForm: function(ctx) { return ""; },
                   //NewForm: function(ctx) { return ""; }
               }
           },
      //     Footer: function(ctx) { return ""; }

      },

      // OnPostRender: function(ctx) { },

      ListTemplateType: 101

    });
  }

  RegisterModuleInit(SPClientTemplates.Utility.ReplaceUrlTokens("~siteCollection/Style Library/sads.js"), init);
  init();

});

There is a Chrome Plugin to develop very easily. You will able to develop/test/preview in live and finally it will be deployed automatically.

  • 1
    Not only test :). I can say I wouldn't be able to use JSLink without Cisar. – Akhoy Oct 22 '15 at 11:08
  • Well.. That code works.. Kinda.. It changes the link so it opens in a new tab.. But it also changes the link to the site it is implemented. So if i open a document in "Http://Intranet/Pics/pics.aspx" It'll change every link for the documents in the document library to "Http://Intranet/Pics/pics.aspx" Also.. Cisar isn't able to save the files that i created. – Tom Oct 22 '15 at 12:20
  • I did not get properly. Please explain more about the problem – Atish Dipongkor - MVP Oct 22 '15 at 12:23
  • If i implement your code whenever i click on one of the documents in a Document libraray itll open a new tab, but in that new tab it wont open the document but the website same again(the website where the document library is) – Tom Oct 22 '15 at 12:27
  • See my updated code. It should be FileRef instead of FileUrl – Atish Dipongkor - MVP Oct 22 '15 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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