Search code examples
google-apps-scriptgoogle-classroom

Attach multiple files to a Google Classroom assignment?


I'm trying to create a Classroom assignment with more than one file attached, here is my code with one file:

  Classroom.Courses.CourseWork.create({
        courseId: id,
        title : title,
        description : desc,
        workType : 'ASSIGNMENT',
        materials: [
          {
            driveFile:{
              driveFile: {
                id: fileId, 
                title: fileName
              },
              shareMode: "STUDENT_COPY"
            },
          }
        ],
        state : "PUBLISHED"
      }, id)

How do I add another file? I have tried to duplicate the 'driveFile' with no luck:

materials: [
      {
        driveFile:{
          driveFile: {
            id: fileId, 
            title: fileName
          },
          shareMode: "STUDENT_COPY"
        },
        driveFile:{
          driveFile: {
            id: fileId, 
            title: fileName
          },
          shareMode: "STUDENT_COPY"
        },
      }
    ],

Can't find any documentation or other questions on this.

Thank you!


Solution

  • Try this code:

    {
      "workType": "ASSIGNMENT",
      "materials": [
        {
          "driveFile": {
            "driveFile": {
              "id": "FILE_ID",
              "title": "TestingFile"
            },
            "shareMode": "STUDENT_COPY"
          }
        },
        {
          "driveFile": {
            "driveFile": {
              "id": "FILE_ID",
              "title": "Sample Docs"
            },
            "shareMode": "STUDENT_COPY"
          }
        }
      ],
      "description": "Assignment 1 &2",
      "title": "Assignment"
    }
    

    Here is the result:

    enter image description here

    I tried it using the Method: courses.courseWork.create Try this API to help me construct the request correctly.

    Hope this helps.