I'm working with SP2010's SP.js and I need to get all the attachments of a list item. As it's SP2010, I don't have access to SP.ListItem's attachmentFiles property. :(
The following code works for this purpose:
var attachmentFolder = clientContext.get_web().getFolderByServerRelativeUrl('/sites/MySite/Lists/MyList/Attachments/' + itemID);
var attachmentFiles = attachmentFolder.get_files();
clientContext.load(attachmentFiles);
clientContext.executeQueryAsync(
function () {
var myAttachments = [];
for (var i = 0; currentItem = attachmentFiles.get_item(i) ; i++)
{
myAttachments.push({
url: currentItem.get_serverRelativeUrl(),
name: currentItem.get_name()
});
}
},
function(error) {
alert("Something has failed magnificently! " + error);
}
);
The issue is that if the list item has no attachments, sp.js throws a "File Not Found" exception into my error callback. Not pictured in the code sample I included, I'm requesting a few other things in this JSOM query, so my solution is unfortunately not as simple as assuming that an error being returned means that no attachments were found. I don't want my lack of attachments to screw up my other logic.
I tried using a method similar to the following:
var query = SP.CamlQuery.createAllItemsQuery();
query.set_folderServerRelativeUrl(folderServerRelativeUrl);
allItems = list.getItems(query);
but it always returns 0 results, even when attachments are present, so I'm assuming that this CAML query only works on folders that are part of a proper library, not backend folders.
I really don't want to have to make an extra request.
Any ideas?