Get the Attachment Name From the List

Dec 4, 2013 at 3:20 PM
Hi,
I have a list which have one attachment for every record. I am trying to get the name of the attachment by code but its not working. Below is my code.


function readList()
{
var fieldsToRead = "<ViewFields>" +
                                    "<FieldRef Name='Title'/>" +
                                    "<FieldRef Name='Attachments'/>"+
                                    "<FieldRef Name='ID'/>" +
                                    "</ViewFields>"; 

var queryStr= "<Query><Where><Neq><FieldRef Name='Title'/><Value Type='Text'>~</Value></Neq></Where></Query>";
$().SPServices({
        operation: "GetListItems", 
        async: false, 
        listName: "MyList", 
        CAMLViewFields: fieldsToRead, 
        CAMLQuery: queryStr, 
        completefunc: function (xData, Status) { 

$(xData.responseXML).SPFilterNode("z:row").each(function() { 

var vName=$(this).attr("ows_Title");
var vFile=$(this).attr("ows_Attachments");
var vID=$(this).attr("ows_ID");
var vAttachment="";

if(vFile>0)
{
vAttachment=theAttachment(vID);
}

else
{
vAttachment="NA";
}

             /* THIS LINE ADD THE DATA TO A TABLE*/
AddRowToTable (vName,vAttachment);
}); } });

}

function theAttachment(theId)
{

$().SPServices({
operation:'GetAttachmentCollection',
listName: "MyList",
ID: theId,
 completefunc: function (xData, Status) {

 var output="";
 $(xData.responseXML).find("Attachments > Attachment").each(function( i, el ) {
    var $node = $(this),
    filePath = $node.text();
return (filePath);

});
}
});
}


The above code is returning UNDEFINED. Please help.
Coordinator
Dec 4, 2013 at 3:36 PM
I think the issue is the training comma on this line:
var $node = $(this),
M.
Dec 4, 2013 at 3:55 PM
Thanks M. I tried with removing the comma with ; but its still not working
Coordinator
Dec 4, 2013 at 4:36 PM
Well, what you have looks basically correct. It's debugging time. Take a look at the network traffic with Firebug or Fiddler to see how it looks.

M.
Dec 4, 2013 at 4:42 PM
It's hard to read the code you've added, but it appears you have the first call as synchronous and subsequent calls to get the AttachmentCollection as asynchronous. Also, the function that is getting the attachment name is returning a value, but it's not doing what you'd expect it to do. The outer function doesn't have a return value, so there's another reason you are getting undefined's.

For this all to work, you'll have to restructure your code, especially if you are expecting multiple attachments.

Cheers,
Matthew