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).
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 :(
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 ); });