0

I need to get URL's and names of attachments from List and I found such code in SPServices documentation. I wonder if there is a way to do the same operation for more than one ID and get separate Div or li elements for each ID:

    $().SPServices({
    operation: "GetAttachmentCollection",
    listName: "MahAwesomeListName",
    ID: id,
    completefunc: function(xData, Status) {
        //console.log( Status );
        //console.log( xData.responseText );

        var output = "";

        //debugger;
        $(xData.responseXML).find("Attachments > Attachment").each(function(i, el) {
            var $node = $(this),
                filePath = $node.text(),
                arrString = filePath.split("/"),
                fileName = arrString[arrString.length - 1];
            output += "<a href='" + filePath + "' target='_blank'>" + fileName + "</a><br />";
        });
        $("#drop-zone").html(output);
    }
});
1
  • You can use GetListItems. On the success method, use a loop and invoke the code you have. Commented Jan 23, 2015 at 11:29

1 Answer 1

0

Here is the code which gets all items in a list and then gets attachments for each item.

$(document).ready(function() {
  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "MahAwesomeListName",
    /*CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",*/
    completefunc: function (xData, Status) {
        var output = "";
        $(xData.responseXML).SPFilterNode("z:row").each(function() {
            var id = $(this).attr("ows_ID");   
            $().SPServices({
                async: false,
                operation: "GetAttachmentCollection",
                listName: "MahAwesomeListName",
                ID: id,
                completefunc: function(xData, Status) {                         
                    $(xData.responseXML).find("Attachments > Attachment").each(function(i, el) {
                        var $node = $(this),
                            filePath = $node.text(),
                            arrString = filePath.split("/"),
                            fileName = arrString[arrString.length - 1];
                        output += "<a href='" + filePath + "' target='_blank'>" + fileName + "</a><br />";
                    });

                }
            });     
        });
        $("#drop-zone").html(output);
    }
  });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.