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]);
        attachmentFiles = attachmentFolder.get_files();
function onGetAttachmentSucc(sender, args) {    
var i=0;
  for(var file in attachmentFiles)
        if(attachmentFiles.itemAt(i) != undefined){             


function onGetAttachmentFail(sender, args) {

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, 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
    Dec 30, 2016 at 14:44

1 Answer 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";

                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++) {

                error: function (data) {

  • I got undefined when I use _spPageContextInfo.siteAbsoluteUrl I use sharepoint 2010
    – Kem Bardly
    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.