How can use the API to add or delete a student from a section. I have already created the section using the API.

Jump to solution
b_c_tompsett
Community Member

I manage a large class (hundreds of students) and it is divided into sections based on days of the week. Some students change days from the "Monday" section to the "Tuesday" section, for example.

I can create sections mechanically using the API but I cannot find in the documentation of the API how to delete or add a student to the section. Obviously it can be done manually through the course "people" tab but for a large class that becomes impractical when frequent changes are made.

I know I can add a student to a section by using (Python syntax - but could be anything really):

#POST /api/v1/sections/:section_id/enrollments
url = "{0}/sections/{1}/enrollments".format(baseUrl,section_id)

extra_parameters={
'enrollment[user_id]': user_id,
'enrollment[type]': 'StudentEnrollment',
'enrollment[enrollment_state]': 'active'
}

But this just add the student  to an additional section, so I needed to have removed the previous section first.

I tried guessing at the syntax:

# Use the Canvas API to get the remove a member from a named section in a course
#DELETE /api/v1/section/:section_id/enrollments
url = "{0}/sections/{1}/enrollments".format(baseUrl,section_id)

extra_parameters={
'enrollment[user_id]': user_id
}

But that does not work and gives me a 404. I could delete the whole section, but it would take many hundreds of enrollments to put everyone back who had not changed!

Is there a syntax for the API section access I need to know?

Labels (1)
0 Likes
1 Solution

Hi @b_c_tompsett,

The enrollment_id will be unique to each user/role/section combination a particular user is enrolled in for a course. So when using enrollment_id with the delete call, only one particular enrollment in a course will be affected and the user would remain enrolled in other sections of the course (or with other roles in the same section).  You will likely need to use one of the list call options to find the enrollment_id you want to delete.

Hope this helps provide a bit more clarity.

-Chris

-Chris

View solution in original post