0

folders

I want to retrieve file Url in all of that folder. I use javascript. Problem is it console the last item. I do not know why. Please help. Thanks

This is code

function checkAttachment(AttachID,getAttachmentStatus){
var itemId = AttachID;
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
for(var i = 0; i<itemId.length; i++){
  if(getAttachmentStatus[i] == true){
        var attachmentFolder=web.getFolderByServerRelativeUrl('Lists/Purchase2/Attachments/'+itemId[i]);
        //console.log(attachmentFolder);
        attachmentFiles = attachmentFolder.get_files();
        console.log(attachmentFiles);
        ctx.load(attachmentFiles);
        ctx.executeQueryAsync(Function.createDelegate(this,this.onGetAttachmentSucc),Function.createDelegate(this,this.onGetAttachmentFail));
        }           
  }
}
function onGetAttachmentSucc(sender, args) {    
console.log(attachmentFiles);                              
var i=0;
  for(var file in attachmentFiles)
  {   
        if(attachmentFiles.itemAt(i) != undefined){             
            console.log(attachmentFiles.itemAt(i).get_serverRelativeUrl());
            i++;
        }

  }
}

function onGetAttachmentFail(sender, args) {
 alert("sorry!");}

sorry for my dirty code T_T

2
  • Why are you making a loop with itemId.length? Is it an array? Could you insert the code here instead of the image?
    – Cecilia
    Commented Dec 30, 2016 at 14:01
  • because the folder is a different path. From the first picture when I attach a file in a form it will create a new folder and add the file to it. it checks by Id. I want to get all file URL from all folder. Do you understand? Sorry for my weak English skill
    – Kem Bardly
    Commented Dec 30, 2016 at 14:44

1 Answer 1

1

Assuming the variable AttachID is an array of item ids, you can do like this (using the REST api):

function checkAttachment(AttachID, getAttachmentStatus) {
    var itemId = AttachID;
    for (var i = 0; i < itemId.length; i++) {
        if (getAttachmentStatus[i] == true) {

            var url = _spPageContextInfo.siteAbsoluteUrl + "/_api/Web/GetFolderByServerRelativeUrl('/Lists/Purchase2/Attachments/" + itemId[i] + "')/Files?$select=ServerRelativeUrl";

            $.ajax({
                url: url,
                type: 'GET',
                headers: { 'Accept': 'application/json;odata=verbose' },
                async: false,
                success: function (data) {
                    var attachments = data.d.results;
                    if (attachments) {
                        for (var j = 0; j < attachments.length; j++) {
                            console.log(attachments[j].ServerRelativeUrl);
                        }
                    }

                },
                error: function (data) {
                    alert("sorry!");
                }
            })

        }
    }
}
1
  • I got undefined when I use _spPageContextInfo.siteAbsoluteUrl I use sharepoint 2010
    – Kem Bardly
    Commented Jan 4, 2017 at 4:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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