2

I'm trying to return the URL of some attachments for a topic. I try to do the following to receive the url:

$(document).ready(function() {  
    GetFiles();

});

    function GetFiles() {
        var method = "GetListItems";
        var list = "Session Schedule";
        var fieldsToRead = "<ViewFields>" +
            '<FieldRef Name="ID"/>'+
            '<FieldRef Name="Title"/>'+
            '<FieldRef Name="LinkFilenameNoMenu"/>'+
            '<FieldRef Name="Created"/>'+
            '<FieldRef Name="EncodedAbsUrl"/>'+
 "<FieldRef Name='Date_x0020_and_x0020_Time' />" + 
 "<FieldRef Name='Attachments'/>" + "</ViewFields>";
        var query = "<Query>" + "<Where>" + "<Or>" + //"<Or>" +
         "<Eq><FieldRef Name=\"Status\" /><Value Type='Text'>Confirmed</Value></Eq>" + 
         "<Eq><FieldRef Name=\"Status\" /><Value Type='Text'>Ready</Value></Eq>" + 
         //"</Or>" + "<Eq><FieldRef Name=\"Status\" /><Value Type='Text'>Completed</Value></Eq>" + 
         "</Or>" + "</Where>" + "<OrderBy>" + "<FieldRef Name=\"Date_x0020_and_x0020_Time\"/>" + 
         "</OrderBy>" + "</Query>";



        $().SPServices({
            operation: method,
            async: false,
            listName: list,
            CAMLViewFields: fieldsToRead,
            CAMLLimit: 1,
            CAMLQuery: query,
            completefunc: function(xData, Status) {
            console.log("xData", xData);
                $(xData.responseXML).SPFilterNode("z:row").each(function() {
                 var url = $(this).attr("ows_EncodedAbsUrl")
                 console.log(url);
                });
            }
        });
    }

The problem is the URL I receive is correct up until the file name, where I get 1_.000, 2_.000 and so on instead of the name of the actual file. The url thus doesn't work.

What am I doing wrong? is it because the attachment is a ppt or am I missing something?

Thanks

1

Should be using GetAttachmentCollection not GetListItems

Can use GetListItems to get the ID from each list item. But to then get their attachments need to use this instead:

http://spservices.codeplex.com/wikipage?title=GetAttachmentCollection

0

First of all, EncodedAbsUrl returns an absolute Url of a File, in case of List Item it has the following format:

http(s)://[site]/[web]/[list]/[item id]_.000

In order to return attachments using SPServices GetListItems operation, the following query option have to be included:

CAMLQueryOptions: "<QueryOptions><IncludeAttachmentUrls>True</IncludeAttachmentUrls></QueryOptions>" 

How to retrieve list items with attachments using SPServices

function getListItems(listName,query,viewFields,rowLimit,complete) 
{
    $().SPServices({
        operation: "GetListItems",
        listName: listName,
        CAMLViewFields: viewFields,
        CAMLLimit: rowLimit,
        CAMLQuery: query,
        CAMLQueryOptions: "<QueryOptions><IncludeAttachmentUrls>True</IncludeAttachmentUrls></QueryOptions>",
        completefunc: function(xData, Status) {
            var items = [];    
            $(xData.responseXML).SPFilterNode("z:row").each(function() {
               items.push($(this).get(0));
            });
            complete(items);
        }
    });
} 

Usage

getListItems('Requests','<Query/>','<ViewFields><FieldRef Name="Attachments"/></ViewFields>',1,printItemAttachments);

function printItemAttachments(items)
{
    for(var i = 0; i< items.length; i++){
        var attachmentsValue = $(items[i]).attr("ows_Attachments");
        if(attachmentsValue == "0") {
           console.log('No attachments'); 
        }
        else {
           var attachmentUrls = attachmentsValue.substr(2,attachmentsValue.length-4).split(';#');    
           console.log(attachmentUrls);   
        }     
    };    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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