In conjunction with migrating users from one source to another (such asMigrating grades from one Canvas course to another ). I found it useful to be able add users based on their SIS ID and to be able set them to inactive or reactivate them.
The results are python programs to enroll a user:
enroll_person_in_course_by_SISID.py course_id SISID role
and a program to inactivate or reactivate the user:
set_enrollment_state_of_SISID_person_in_course.py course_id SISID state
Examples with pictures and code are at activating and reactivating users: Chip sandbox
Initially I was enrolling the user with a given state, but this has the undesirable side effect of putting them in the default section and not just changing their state (if they are already enrolled). Looking at what the GUI sends when deactivating and reactivating a user let me to the solution adopted in the two programs above. The main gotcha was that you have to use the enrollment_id and not the sis_id or user_id, this means that given the user's SIS ID you first have to lookup the user to find their enrollment_id. Unfortunately, the GET /api/v1/courses/:course_id/enrollments state parameter does not seem to support an array of values, but rather has to be a string with one of the values; this means that one has to lookup inactive users separately from active users. It would be desirable if the state could be a vector of values or is there was synthetic state "all". Even more confusing as the parameter name is state , which to me suggests that it has an array value.
Now what caused me to do the above? A question came up about what to do with users that could be migrated, but whose status was that they had interrupted their studies in the course or for some other reason could not complete the course. I would like to thank Brian Neporadny for his answer in When do you archive / remove / disable users and content? that lead to exploring the idea of adding these users, adding their grades, and then setting their state to inactive. In this way the instructor can see the grades of the student for those assignments that did have a reported result, while not allowing the student access to the course. [For testing purposes it was useful to be able to reactivate a user after setting them inactive.]