0

I have a list in sharepoint 2013 and created an html form and script which will insert data and upload a document into this list. Now for second level, i want to fetch and only view all these saved data which i created. I used jsom to fetch all the records

But problem is with attachment- How to get attachment document into form or download it to local from this list. Not finding any good resource in google. Can any one please help me?.

Analyst
  • 3
  • 2

1 Answers1

0

You could use JSOM to get the attachement files.

Sample script:

<script type="text/javascript" src="/SiteAssets/jquery-3.4.1.js"></script>
    <script type="text/javascript">
        function getListItemAttachements() {
            //replace the list and id dynamically
            getAttachements("Test", 1);
        }
        function getAttachements(listName, itemID) {
            var attachmentFiles;
            var ctx = new SP.ClientContext.get_current();
            var web = ctx.get_web();
            var attachmentFolder = web.getFolderByServerRelativeUrl('Lists/' + listName + '/Attachments/' + itemID);
            attachmentFiles = attachmentFolder.get_files();
            ctx.load(attachmentFiles);
            ctx.executeQueryAsync(function () {
                for (var j = 0; j < attachmentFiles["$2_1"].length; j++) {
                    var file = attachmentFiles.itemAt(j);
                    console.log(file.get_name());
                }

            }, function (err) {
                console.log(err);
            });
        }
    </script>
    <input id="Button1" onclick="getListItemAttachements()" type="button" value="button" />
Lee
  • 5,305
  • 1
  • 6
  • 12
  • It worked for me. small change. attachmentFiles["$2_1"].length is giving error so used emumerator. – Analyst Jul 09 '20 at 09:33
  • This code will give the file name but how to download this file? All the details has been retrieved now with attachment name also.When user click on this attachment, it should be downloaded. – Analyst Jul 09 '20 at 09:42
  • You could get the file url by file.get_serverRelativeUrl and download based on url. – Lee Jul 09 '20 at 09:49
  • got it..i was using serverRelativeurl only that's why showing error. Can i use any Include attachment to load in context- created seperate code for attachment. I am using below code- this.collListItem = oList.getItems(camlQuery); clientContext.load(collListItem); – Analyst Jul 09 '20 at 10:25
  • Thank you for the solution. As per your code, we would need to load attachment again in context. Is there any way we could load it with other fields records and get it done. – Analyst Jul 09 '20 at 10:29