To Our Amazing Educators Everywhere,
Happy Teacher Appreciation Week!
Found this content helpful? Log in or sign up to leave a like!
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?
Solved! Go to 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
Hi @b_c_tompsett,
You'll want to check out the Enrollment API to accomplish those tasks. There is a call for creating an enrollment and a separate call for deleting an enrollment. In general you'll need to know at least the canvas section_id and the user_id to create an enrollment, and the course_id and the enrollment_id to delete an enrollment.
Hope this helps you out a bit!
-Chris
Thank you for your reply. As I showed in my code sample, I am using the Enrollment API to successfully enroll students in a section using the section_id and the user_id.
Unless I have misunderstood your pointer, using a course_id and enrollment_id to delete an enrollment would remove the student from the course and not just the section. The student is still required to be live on the course, and only the section changes.
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
Thank you. That was the bit of magic I needed. I had not appreciated the subtle differences between 'id', 'user_id' and 'enrollment_id' in different contexts!
The following code worked just fine as the enrollment API specification indicated. I just did not fully understand the underlying data structures.
#DELETE /api/v1/courses/:course_id/enrollments/:id
url = "{0}/courses/{1}/enrollments/{2}".format(baseUrl,course_id,enrollment_id)
To participate in the Instructure Community, you need to sign up or log in:
Sign In