1

My questions refers to this article, document preview in edit form (Add document preview to Document library form)

I want to use this script in my display form (DispForm.aspx), but the variables will not get filled, they are still undefined.

Thats the code.

$(document).ready(function () { var strDocNameEncoded = encodeURI($('input[id^="FileLeafRef"]').val()); var strDocExt = $('input[id^="FileLeafRef"] + span').html(); if (strDocExt=='.doc' || strDocExt=='.docx' ||strDocExt=='.xls' || strDocExt=='.xlsx' ) {

var strDocUrl = "https://yourdomain.sharepoint.com/sites/dev/_layouts/15/WopiFrame.aspx?sourcedoc=https://yourdomain.sharepoint.com/sites/dev/PreviewFormDocLib/" + strDocNameEncoded + strDocExt + "&action=default"; } else { var strDocUrl = "https://yourdomain.sharepoint.com/sites/dev/PreviewFormDocLib/" + strDocNameEncoded + strDocExt; } $('#LSViewDocInTask').prop('src', strDocUrl ); });

I guess the field names are not available in display form.

This is the browser source code from the Page. I think the easiest way or one possiblitly is to extract the direct link to the document is the existing link (yellow marked).

enter image description here

Could you help me to extract URL /sites/showroom/teamseite/Prozesse/Prozess%20Beispiel%2002.docx from screenshot source code?

I tried it again with ctx.CurrentItem.FileLeafRef.FileUrl as variable, but no success :(

enter image description here

SOLVED

Here is a solution for the code (like Add document preview to Document libary form)

$(document).ready(function () { var strDocNameEncoded = encodeURI($("a[rel|='sp_DialogLinkNavigate']" ).text()); var strDocExt = strDocNameEncoded.split('.').pop(); if (strDocExt=='.doc' || strDocExt=='.docx' ||strDocExt=='.xls' || strDocExt=='.xlsx' ) {

var strDocUrl = "https://yourdomain.sharepoint.com/sites/_layouts/15/WopiFrame.aspx?sourcedoc=https://yourdomain.sharepoint.com/sites/dev/" + strDocNameEncoded + strDocExt + "&action=default"; } else { var strDocUrl = "https://yourdomain.sharepoint.com/sites/dev/" + strDocNameEncoded "+ "." + strDocExt; } $('#LSViewDocInTask').prop('src', strDocUrl ); });

  • You really need to add more info to your question – Morten K Oct 4 '18 at 9:37
  • Sorry, that was too fast. I modified my post right now. – corey Oct 4 '18 at 9:39
0

The problem is that the script is looking for "input" fields which by definition does not appair in display forms (As you cant edit anything in the displayform).

You would need to get the correct element from the DOM. Now the issue here being that fields aren't labelled with an (Unique) ID so you would need some other way to get the element you're trying to modify.

The easiest way I can think of right now would be to look for any <a>-tags which name propperty ends on your fields name (Like this: SPBookmark_Title). Then traverse up to the nearest <td>-tag (Usually two steps up). Then get the next sibling and finally get the value from that element.

I'm not super experienced in javascript/jquery so it could be there's an esier way to do it that I don't know of.

  • Yes, thats right! Thanks! I had a closer look to the code and I thinkt the easiest way is to extract the existing <a> link behind the Name in Displayform, it is exactly the same link which I want to use in my web part for showing the document preview. – corey Oct 4 '18 at 12:56
  • Glad it helped pointing you in the right direction :) – Morten K Oct 5 '18 at 5:01
  • But I am still standing in fog – corey Oct 7 '18 at 0:12
  • You would have to either get into javascript or perhaps see if anybody in your oganisation is able to write a script for you. – Morten K Oct 8 '18 at 5:07
  • I have a solution found encodeURI($("a[rel|='sp_DialogLinkNavigate']" ).text()); – corey Oct 8 '18 at 9:34
0

I solved it by editing the code from (Add document preview to Document library form) to this:

$(document).ready(function () { var strDocNameEncoded = encodeURI($("a[rel|='sp_DialogLinkNavigate']" ).text()); var strDocExt = strDocNameEncoded.split('.').pop(); if (strDocExt=='doc' || strDocExt=='docx' ||strDocExt=='xls' || strDocExt=='xlsx' ) {

var strDocUrl = "https://winitux.sharepoint.com/sites/showroom/teamseite/_layouts/15/WopiFrame.aspx?sourcedoc=https://winitux.sharepoint.com/sites/showroom/teamseite/Prozesse/" + strDocNameEncoded + "&action=default"; } else { var strDocUrl = "https://winitux.sharepoint.com/sites/showroom/teamseite/Prozesse/" + strDocNameEncoded; } $('#LSViewDocInTask').prop('src', strDocUrl ); });

"a[rel|='sp_DialogLinkNavigate']" )

helps me to get the FileLeafRef name and the document title (URI).

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.