none
Reference Attachment Column in SharePoint List? RRS feed

  • Question

  • Hi,

    I need a way to grab the URL from the attachment on a list item?

    Halp please :D 

    Thanks,

    Daniel Margison 


    Daniel Margison

    Friday, March 31, 2017 12:25 AM

Answers

All replies

  • Hi,

    If you want to get file URL when user upload the file, you could do this by JavaScript.

    http://stackoverflow.com/questions/15201071/how-to-get-full-path-of-selected-file-on-change-of-input-type-file-using-jav

    If you want to retrieve attachments of list item, you could try with REST api.

    /_api/web/lists/getByTitle('Employee')/items(1)?select=AttachmentFiles,Title&$expand=AttachmentFiles

    http://sharepoint.stackexchange.com/questions/111185/sharepoint-rest-api-attachmentfiles-from-a-collection-of-list-items

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, April 3, 2017 2:52 AM
  • With the help of JSOM code, you can get the list attachments for particular item

    function GetAttachments (listName, itemId) {
    
        var url = _spPageContextInfo.webAbsoluteUrl;
        var requestUri = url + "/_api/web/lists/getbytitle('" + listName + "')/items(" + itemId + ")/AttachmentFiles";
        var str = "";
        // execute AJAX request
    
        $.ajax({
            url: requestUri,
            type: "GET",
            headers: { "ACCEPT": "application/json;odata=verbose" },
            async: false,
            success: function (data) {
                for (var i = 0; i < data.d.results.length; i++) {
                    str += "<a href='" + data.d.results[i].ServerRelativeUrl + "'>" + data.d.results[i].FileName + "</a>";
                    if (i != data.d.results.length - 1) {
                        str += "<br/>";
                    }
                }
            },
            error: function (err) {
                //alert(err);
            }
        });
    
    
        return str;
    }


    Thanks, Amit Kumar, LinkedIn Profile ** My Blog

    Friday, April 7, 2017 7:54 AM
  • Hi Daniel

    Just right click on the attachment and click "copy shortcut".

    The url will be of following format.

    <siteURL>/Lists/<ListName>/Attachments/1/Usage.xlsx?Web=1

    Cheers

    Reshmee

    Friday, April 7, 2017 12:59 PM
  • Sorry, I need to get the URL of the attachment, not the file

    Daniel Margison

    Thursday, April 20, 2017 4:12 AM
  • I need to get thie programatically for every item

    Daniel Margison

    Thursday, April 20, 2017 4:12 AM
  • Hi,

    Here is sample code for your reference:

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.7.1.min.js"></script>
        <script type="text/javascript">
            $(function () {
                var listName = 'Employee';
                $.ajax({
                    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('" + listName + "')/items?$select=ID",
                    type: "GET",
                    headers: { "accept": "application/json;odata=verbose" },
                    success: (function (data, textStatus, jqXHR) {                    
                        $.each(data.d.results, function (key, value) {
                            var ID = value.ID;
                            $.ajax({
                                url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('" + listName + "')/items(" + ID + ")?select=AttachmentFiles,Title&$expand=AttachmentFiles",
                                type: "GET",
                                headers: { "accept": "application/json;odata=verbose" },
                                success: (function (data, textStatus, jqXHR) {
                                    $.each(data.d.AttachmentFiles.results, function (key, value) {
                                        alert(value.ServerRelativeUrl);
                                    });
                                }),
                                error: (function (xhr, ajaxOptions, thrownError) {
                                    alert('Request failed: ' + xhr.status + '\n' + thrownError + '\n' + xhr.responseText);
                                })
                            });
                        });                    
                    }),
                    error: (function (xhr, ajaxOptions, thrownError) {
                        alert('Request failed: ' + xhr.status + '\n' + thrownError + '\n' + xhr.responseText);
                    })
                });
            });
        </script>

     

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, April 20, 2017 5:14 AM
  • I'm not very good with JS. Where will this output the URL? Can it display on a page? And do I have to modify any of the code?

    Thanks

    Daniel Margison

    Thursday, April 20, 2017 5:38 AM
  • Hi,

    I add some comment so it may help you understand the code , and you could test the code by add a script editor web part to a page and then insert below code into it.

     <script type="text/javascript" src="https://code.jquery.com/jquery-1.7.1.min.js"></script>
        <script type="text/javascript">
            $(function () {
                //the list which you want to query, replace with your own list name.
                var listName = 'Employee';
                $.ajax({
                    // use below REST api to retrive all list items ID 
                    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('" + listName + "')/items?$select=ID",
                    type: "GET",
                    headers: { "accept": "application/json;odata=verbose" },
                    success: (function (data, textStatus, jqXHR) {                    
                        $.each(data.d.results, function (key, value) {
                            // get each item ID in order to get attachements
                            var ID = value.ID;
                            var listItemInfo = "";
                            $.ajax({
                                //use below URL to get attachments
                                url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('" + listName + "')/items(" + ID + ")?select=AttachmentFiles,Title&$expand=AttachmentFiles",
                                type: "GET",
                                headers: { "accept": "application/json;odata=verbose" },
                                success: (function (data, textStatus, jqXHR) {                                
                                    $.each(data.d.AttachmentFiles.results, function (key, value) {
                                        //attachment url(like /Lists/Employee/Attachments/3/filename.xlsx), you may add your logic to process.
                                        listItemInfo = "<strong>ID: " + ID + " </strong><strong>URL: " + value.ServerRelativeUrl + " </strong><br />";
                                        $("#listItems").append(listItemInfo);
                                    });                                
                                }),
                                error: (function (xhr, ajaxOptions, thrownError) {
                                    alert('Request failed: ' + xhr.status + '\n' + thrownError + '\n' + xhr.responseText);
                                })
                            });                        
                        });                    
                    }),
                    error: (function (xhr, ajaxOptions, thrownError) {
                        alert('Request failed: ' + xhr.status + '\n' + thrownError + '\n' + xhr.responseText);
                    })
                });
            });
        </script>
    
        <div id="listItems"></div>

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, April 20, 2017 6:22 AM
  • Hi,

    I am checking to see how things are going there on this issue. Please let us know if you would like further assistance.

    You can share your own solution here and mark it as answer if it had been solved, so it may help others who has similar requirement.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, April 25, 2017 5:22 AM