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 ); });

2
  • You really need to add more info to your question Commented Oct 4, 2018 at 9:37
  • Sorry, that was too fast. I modified my post right now. Commented Oct 4, 2018 at 9:39

2 Answers 2

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.

5
  • 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. Commented Oct 4, 2018 at 12:56
  • Glad it helped pointing you in the right direction :) Commented Oct 5, 2018 at 5:01
  • But I am still standing in fog Commented Oct 7, 2018 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. Commented Oct 8, 2018 at 5:07
  • I have a solution found encodeURI($("a[rel|='sp_DialogLinkNavigate']" ).text()); Commented Oct 8, 2018 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 and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.