Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

How can I fetch the attachments (the URL, to be precise) of a list item with SPServices (the jQuery library)?

share|improve this question
add comment

2 Answers

This should get you where you need to go:

ExecuteOrDelayUntilScriptLoaded(getWebProperties, "SP.js");            
var attachmentFiles;

function getWebProperties() {
    var itemId=2;
    var ctx = new SP.ClientContext.get_current();
    var web = ctx.get_web();
    var attachmentFolder=web.getFolderByServerRelativeUrl('Lists/City/Attachments/'+itemId);
    attachmentFiles= attachmentFolder.get_files();
    ctx.load(attachmentFiles);              
    ctx.executeQueryAsync(Function.createDelegate(this,this.onSuccess),Function.createDelegate(this,this.onFailed));
}

function onSuccess(sender, args) {                                      
    var i=0;
    for(var file in attachmentFiles) {                              
        alert(attachmentFiles.itemAt(i).get_serverRelativeUrl());
        i++;
    }
}

function onFailed(sender, args) {
    alert("sorry!");
}
share|improve this answer
add comment

I just wanted to amend an alternate formation of the onSuccess function that worked better for me:

function onSuccess(sender, args) {                                      
    var i = 0;
    while (attachmentFiles.itemAt(i)) {
        var file = attachmentFiles.itemAt(i).get_serverRelativeUrl();
        alert(file);
        i++;
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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