cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
seanh
New Member

How can I get a user's sections for a course, from the Canvas API?

How can I get a list of all of a given user's sections for a given course, including both the ID and name of each section (not just the IDs), from the Canvas API?

Importantly the user in question is not the authenticated user, it's an arbitrary user that's different from the user who I'm authenticating the API as.

Ideally we'd like to be able to do this in a single API call, since we need to get this info on every LTI launch and the more API calls we need to make during our launch process the longer the user has to wait.

 

So far I've been able to figure out that you can get a list of the authenticated user's section like this:

http GET "https://FOO.instructure.com/api/v1/courses/<COURSE_ID>?include[]=sections" Authorization:"Bearer 9382~c7I***A8v"

You can get a list of all sections in a course like this:

http GET "https://FOO.instructure.com/api/v1/courses/<COURSE_ID>/sections" Authorization:"Bearer 9382~p5X***L9y"

You can even get a list of an arbitrary user's (not the authenticated user's) sections like this, but it includes only the section IDs not the names:

http "https://FOO.instructure.com/api/v1/courses/<COURSE_ID>/users/<USER_ID>?include[]=enrollments" Authorization:"Bearer 9382~1sN***oaZ"

But none of those are what I need. So far, I've been unable to get the list of course sections for a user other than the authenticated user and with the section names not just the IDs.

Does anyone know of a way to do this?

Thanks

0 Kudos
5 Replies
ColinMurtaugh
Community Champion

Hey Sean --

I think your best bet might be to try using the new GraphQL API. I don't have any experience with GraphQL beyond poking around a bit in the Explorer, but I think you should be able to get a user's enrollments including the section names. 

The GraphQL Explorer can be found at https://FOO.instructure.com/graphiql and there is documentation available at # GraphQL API - Canvas LMS REST API Documentation

I think the trickiest bit might be that the GraphQL API has different IDs for all of the objects than the ones that are used in the REST API. For example, my own Canvas REST user ID is 7, but in GraphQL my ID is an 8-character alphanumeric string. 

I found my GraphQL user id with this query:

query MyQuery {
legacyNode(_id: "7", type: User) {
id
}
}

and then I was able to get my own enrollments, including the section names, with this GraphiQL query: 

query MyQuery {
node(id: "my-8-char-ID-here") {
... on User {
id
email
enrollments {
_id
section {
_id
id
name
}
}
}
}
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

There may be a way to do both of those things in one call, or there may be another way to get users' GraphQL IDs altogether.

Hope this helps!

--Colin

0 Kudos
xcotto
Community Contributor

Hello  @seanh ‌,

I believe with this API Courses - Canvas LMS REST API Documentation  you can achieve your request, now if the user has a lot of enrollments you might need to include &page=2 and so on until no values return. 

API Example: https://foo.instructure.com/api/v1/users/5037/courses?state[]=available&enrollment_state[]=active&in...

I hope it helps in some way...

Xavier Cotto :smileycool:

0 Kudos
bbennett2
Community Champion

Just a heads up that the most you can request per page is undocumented. The most I typically can get at a time is 100.

0 Kudos
xcotto
Community Contributor

Yes that is true, I've added a digit by mistake.

thanks for the catch  @bbennett2 ‌

0 Kudos
bbennett2
Community Champion

Now, if I could grab 1000 resources at once, that would make a lot of my code much simpler

0 Kudos