10

I need to create form to enter metadata for newly uploaded document in document library in SharePoint Online. In that form user should be able to see preview of uploaded document. Actually, user should see document in preview and write down necessary data into fields. Documents could be Office or PDF files.

Should I create new EditForm.aspx in SharePoint Designer? What code should I write there? Where should I place that code?

8

I've found simple solution. Here is step-by-step guide:

  1. Download jquery.min.js and put it, for example, in Assets library.

  2. Open Document library (one where you want to have preview on editform) and on ribbon click Library - Form Web Parts - Defalut Edit Form

  3. On Edit Form page click Add a Web Part and insert Content editor Web Part.

  4. Click Edit Source and add following code:

    <div id="OpenRelativeCard" style="margin-left:-10px;">
                <iframe id="LSViewDocInTask" style="width: 700px; height: 800px;"></iframe>
    </div>
    
  5. On Edit Form page click Add a Web Part and insert Script editor Web Part

  6. Click Edit Snippet and add following code (replace URLs with yours):

    <script language="javascript" type="text/javascript" src="/sites/dev/PreviewFormDocLib/jquery.min.js"></script>
    <script type="text/javascript">
    $(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 );
    });
    </script>
    
  7. Make sure that you arrange Web Parts in following sequence:

    1) Metadata fields

    2) Content Editor

    3) Script Editor

It's important to place Content Editor above Script Editor

  1. Click on ribbon Page - Stop Editing - Stop Editing

Here is result: Document Preview on Form in Document library

  • Sadly this solution doesn't appear to work any more. The Wopi frame URL format expects a documentid parameter. – Panoone Dec 15 '16 at 0:14

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.