Sharepoint 2013

Get Attachments using SPServices in SharePoint 2013

Sharing is Caring

[sourcecode language=”javascript”]

$().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());
});
}

});
});
}
});[/sourcecode]

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

[sourcecode language=”javascript”]
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
}

});

});
[/sourcecode]

Have fun 🙂

Get Attachments using SPServices in SharePoint 2013” üzerine 5 yorum

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

    [sourcecode language=”xml”]
    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
    }

    });

    });
    [/sourcecode]
    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. Hey Marc,

    Thanks for publishing your code.Its useful indeed 🙂 Will try it and update my code according to your suggestion.Much appreciated!

    Cheers,
    Have a nice day

  3. 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’;
    }

Bir Cevap Yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir