Using opening links using document library feature

Forums vLooup for SharePoint Using opening links using document library feature

Viewing 12 reply threads
  • Author
    Posts
    • #27050
      chgrnet
      Participant

      Some time back I think you gave me some code that allowed me to define certain files shown using the “document library” feature with vLookup to open these files as files, not take the browser over. This works perfectly.

      I am using FileLeafRef as the link to open the file, and some files are images. Is there a way to make any of the links (including images) visible open in a new window and not takeover the browser?

    • #27071
      Alexander Bautz
      Keymaster

      To help you I need to see the code you are using now – can you attach it here?

      Please ensure you don’t include any sensitive information in the code snippet.

      Alexander

      • #27073
        chgrnet
        Participant

        I just sent you an email with the code I am using to force certain file types to download.

        The title of this post is incorrect, so sorry about that. It should be: Opening links in a new window using the document library feature

      • #27075
        chgrnet
        Participant

        Your emailed code worked perfectly, thank you!

    • #27252
      HYM
      Participant

      hi, can you please share the script?
      thank you

    • #27407
      Alexander Bautz
      Keymaster

      Use this in the Special configuration textarea in your vLookup setup:

      {"function":"showDownloadBtn"}

      Then add this to your forms (all forms where you use vLookup) and if you use vLookup in a list view also add the function to the view (wrap it in a <script> tag and put it in a script editor web part in the page):

      function showDownloadBtn(a, b) {
          var c = [], iArr = ["doc", "docx", "xls", "xlsx", "pptx"], v = b.get_item("FileLeafRef"), ext = v.split(".").pop();
          if ($.inArray(ext, iArr) > -1) {
              // Proper method for other browsers - will open it in IE and not trigger a download
              c.push("<a href='" + b.get_item("FileDirRef") + "/" + v + "' download>Download</a>");
          }else{
              // All other documents open in a new tab
              c.push("<a href='" + b.get_item("FileDirRef") + "/" + v + "' target='_blank'>"+ v +"</a>"); // This will show the doc name - replace "+ v +" with the text Download if you like
          }
          return c.join("");
      }

      Alexander

    • #30269
      Paul Lynch
      Participant

      Hi Alex,

      Believe this is related to this post, am trying to do the opposite thing than this lady.

      When a user clicks on the Name of a file in the vlookup in a form, I want it to open the excel file in excel online, not in a _target=blank tab (which causes the file to download).

      This mimics the default SP Doc library view where if you click on the file name it opens in O365 (excel online) does not download.

      Setup – using Chrome btw and latest version of DFFS. (although would like it to force this behaviour in IE/Edge too)

      list1 and a vlookup to library1 – library1 contains excel files.

      In my list form, the vlookup uses FileLeafRef to show a link to open the excel document.

      (LinkFilenameNoMenu,LinkFilename are computed and do not appear as links)

      Guessing it might be a special function like above I need to add?

      KR
      Paul

      • This reply was modified 3 years, 9 months ago by Paul Lynch.
      • This reply was modified 3 years, 9 months ago by Paul Lynch.
    • #30274
      Paul Lynch
      Participant

      example of where where I am clicking

      Attachments:
      • #30282
        Alexander Bautz
        Keymaster

        Hi,
        You can do that by adding this to your Special configuration:

        {"function":"openInWeb"}

        and this to your Custom JS:

        function openInWeb(val, item){
            var name = item.get_item("FileLeafRef");
            var url = item.get_item("FileRef");    
            return "<a href='"+url+"?web=1' target='_blank'>"+name+"</a>";
        }

        Let me know how this works out.

        Alexander

        PS: This only works in Office 365.

    • #30286
      Paul Lynch
      Participant

      This works perfectly thank you Alex

    • #30897
      Paul Lynch
      Participant

      Sorry Alexander, I have a separate vLookup, with pdf’s only. Obviously not O365 MS application.

      Is there anyway to force these to download (rather than open in browser), or even better open in Adobe (or prompt a choice)?

    • #30922
      Alexander Bautz
      Keymaster

      If you use old versions of IE or Edge this won’t work, but you can try modifying the link and add “download” like this:

      return "<a href='"+url+"' download>"+name+"</a>";

      This should automatically download it.

      I cannot help with auto-opening in Adobe (unless it is the browser plugin – then clicking the link you already had should do it).

      Alexander

    • #31192
      Paul Lynch
      Participant

      Thanks for this Alexander, and good news about MS backing down.

      My vLookup (configured within FORM1) uses a document library of excel files (FORM2)

      Is it possible to;
      – When user clicks to edit the vlookup item in table (FORM1)
      – Get a pop up and see the Editform (FORM2) of the doc library item
      – Within FORM2 pop up, have a html field, or script the Name field, so it becomes a link that opens excel in a new tab?

      The “Name” field doe not do this, unless it is in display form (although it downloads it rather than opens in a tab).

    • #31202
      Alexander Bautz
      Keymaster

      In your Document library EditForm add a HTML section with this div inside:

      <div id="docLinkPlaceholder"></div>

      Then add this to your Custom JS:

      (function (){
          var item = spjs.utility.getItemByID({
              "listName":_spPageContextInfo.pageListId,
              "id":spjs.dffs.data.thisItemID,
              "viewFields":["ID","FileLeafRef","EncodedAbsUrl"]
          });
          jQuery("#docLinkPlaceholder").html("<a href='"+item.EncodedAbsUrl+"?web=1' target='_blank'>"+item.FileLeafRef.split(";#")[1]+"</a>");
      })();

      Alexander

    • #33701
      Brian Oster
      Participant

      I was having the same issue as Paul in that I need to be able to click the link and edit the document and save changes back to the document in the list. The openInWeb function worked great for this.

      However, is there a way to have it open in the Office Desktop App instead of the Web. I am not talking about the default behavior which is to download the document and then effectively edit it locally. I want the default behavior when viewing in native SP where you have the option to “Open in Word” or “Open in Excel” so that edits are saved back to the document in the doc lib. This way I can give users effectively the same option as “Open in Excel” or “Open in Excel Online”.

    • #33707
      Brian Oster
      Participant

      Ok, I think this works.

      
      
      function openInWeb(val, item) {
          var name = item.get_item("FileLeafRef");
          var url = item.get_item("FileRef");    
          return "<a href='" + url + "?web=1' target='_blank'>" + name + "</a>";
      }
      
      function openInDesktopApp(val, item){
          //debugger;
          var retlink = "";
          var name = item.get_item("FileLeafRef");
          var doctype = name.substring(name.lastIndexOf('.') + 1).toLowerCase();
          var url = makeAbsUrl(item.get_item("FileDirRef") + "/" + name);
      
          switch(doctype) {
              case "doc" :
              case "docx" :
                  retlink = "<a href='ms-word:ofe|u|" + url + "'>" + name + "</a>";
                  break;
              case "xls" :
              case "xlsx" :
                  retlink = "<a href='ms-excel:ofe|u|" + url + "'>" + name + "</a>";
                  break;
              default:
                  retlink = openInWeb(val, item);
                  break;
          }
          
          return retlink;
      }
      • This reply was modified 2 years, 9 months ago by Brian Oster.
      • This reply was modified 2 years, 9 months ago by Brian Oster.
      • This reply was modified 2 years, 9 months ago by Brian Oster.
Viewing 12 reply threads
  • You must be logged in to reply to this topic.