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

  • Why are you making a loop with itemId.length? Is it an array? Could you insert the code here instead of the image? – Cecilia Dec 30 '16 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 Dec 30 '16 at 14:44
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!");
                }
            })

        }
    }
}
  • I got undefined when I use _spPageContextInfo.siteAbsoluteUrl I use sharepoint 2010 – Kem Bardly Jan 4 '17 at 4:20

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.