ModuleItem Limitation

Jump to solution
daniela_aguado
Community Participant

Hello Fellow Canvas Developers,

I'm an amateur coder and I need your help! I'm trying to get/list all the moduleItem objects within a module into an array. I'm only able to load the first 10 moduleItems into an array. In the module there are more than 10 items . How can I load all the moduleItems into the array?

My code looks like this:

 

 

  // Make API request to get the list of modules
  var modulesResponse = UrlFetchApp.fetch(modulesApiUrl, {
    headers: {
      Authorization: 'Bearer ' + accessToken
    }
  });

  // Parse the response as JSON
  var modules = JSON.parse(modulesResponse.getContentText());

  // Create a new Google Sheet and set headers
  var sheet = SpreadsheetApp.create('Canvas Modules List with Items 2');
  sheet.getRange('A1:H1').setValues([['ID', 'Name', 'Position', 'Items Count', 'Published', 'Item ID', 'Item Name', 'Item Type']]);

  // Populate the sheet with module and item data
  for (var i = 0; i < modules.length; i++) {
    var module = modules[i];

    // Fetch items for the current module
    var itemsApiUrl = module.items_url;
    var itemsResponse = UrlFetchApp.fetch(itemsApiUrl, {
      headers: {
        Authorization: 'Bearer ' + accessToken
      }
    });
    var items = JSON.parse(itemsResponse.getContentText());

    // Populate the sheet with module data
    for (var j = 0; j < items.length; j++) {
      var item = items[j];
      sheet.appendRow([
        module.id, module.name, module.position, module.items_count, module.published,
        item.id, item.title, item.type
      ]);
    }
  }

  Logger.log('Modules list with items has been fetched and added to the Google Sheet.');
}

 

 

 

Seems like there's a limit of 10 moduleItems that can be loaded to the items array. If a module within the course has 49 items, how can the items array load the 49 items?

Attached is the output of the above code.

Any shed of light or ideas would be much appreciated. Thank you!

0 Likes
1 Solution
dgrobani
Community Champion

The API by default returns 10 items at a time, so you'll need to account for that (it's referred to as pagination). Here's a long discussion about it that includes examples of how to handle it in various languages.

View solution in original post