Posted in Sharepoint 2013

Get Attachments using SPServices in SharePoint 2013


$().SPServices({

operation: "GetListItems",
async: false,
listName: "listname",
completefunc: function (xData, Status) {
$(xData.responseXML).SPFilterNode("z:row").each(function() {
$().SPServices({
operation:'GetAttachmentCollection',
listName: "listname",
ID: $(this).attr("ows_ID"),
completefunc: function (xData, Status) {
$(xData.responseXML).find("Attachments").each(function() {
alert($(this).text());
});
}

});
});
}
});

But theres a much more better way to do it,Thanks to Marc:

var p = $().SPServices({
    operation: "GetListItems",
    listName: "listname",
    CAMLQueryOptions: "<QueryOptions>" +
            "<IncludeAttachmentUrls>TRUE</IncludeAttachmentUrls>" +
        "</QueryOptions>"
});
 
p.done(function() {
 
    $(p.responseXML).).SPFilterNode("z:row").each(function() {
     
        var attachments = [];
 
        var att = $(this).attr("ows_Attachments");
        if(att !== "0") {
            attachments = att.split(";#"); // Now you'll have an array of attachment URLs
        } else {
            // att will be "0", indicating that there are no attachments
        }
     
    });
 
});

Have fun 🙂

Advertisements

5 thoughts on “Get Attachments using SPServices in SharePoint 2013

  1. There’s actually a little better way to do this. I stumbled on it somewhere along the way.

    var p = $().SPServices({
    	operation: "GetListItems",
    	listName: "listname",
    	CAMLQueryOptions: "<QueryOptions>" +
    			"<IncludeAttachmentUrls>TRUE</IncludeAttachmentUrls>" +
    		"</QueryOptions>"
    });
    
    p.done(function() {
    
    	$(p.responseXML).).SPFilterNode("z:row").each(function() {
    	
    		var attachments = [];
    
    		var att = $(this).attr("ows_Attachments");
    		if(typeof att === object) {
    			attachments = att.split(";#"); // Now you'll have an array of attachment URLs
    		} else {
    			// att will be "0", indicating that there are no attachments
    		}
    	
    	});
    
    });
    

    This gets it all done in one call rather than n+1 calls. (One to get the items and then one additional call to get the attachments for each item.)

    Hope this is useful!

    M.

  2. Hi, checking the resulting attachments array I noticed that the first and last elements are empty since $(this).attr(“ows_Attachments”) contains a string like “;#AttachmentURL;#” which results in a 3 elements array, so I sliced the array to delete the first and last elements:

    var attachments = [];
    var att = $(this).attr(“ows_Attachments”);

    if(att !== “0”) {
    attachments = att.split(“;#”);
    attachments = attachments.slice(1, attachments.length -1);
    } else {
    attachments[0]=’some address of a generic image’;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s