We have a large account with over a hundred subaccounts. We would like to be able to retrieve all subaccount admin roles for a given user over the API, without having to cycle through every subaccount and list the users. Is this possible? When I go to https://columbiasce.test.instructure.com/api/v1/users/ I don't get any admin information. Thanks!
Solved! Go to Solution.
True, unfortunately I have not been able to derive a solution that does this collectively in very few API calls. Fortunately it does not take too long to run. The problem with this example (as stated, it IS a rudimentary example) is if you had to run this (or any API script) constantly against your instance, then yes the number of API calls would have a more significant impact on performance.
The other issue is Canvas only supports (I think) the option to query the admins for a single subaccount at a time, so one would have to iteratively loop through an array of account IDs in order to return the admins for all subaccounts.
I'm not sure of the answer to this question, but I'm going to share it with the Canvas Developers and Canvas Admins groups in the Community to see if they can help.
Thanks, Kona. I thought I was sharing it with the Canvas Developers, but I'm new to this forum!
Nope, this was posted in the general "Find Answers" area of the Community and didn't show up that it had been shared with any other groups. No worries though, that's what we're here for is to help out with the Community and Community members! 🙂
If you haven't already, you should consider joining the Developers group so you have access to all of their resources and information!
Good afternoon,
The following PowerShell script might help for you. It assumes your subaccounts belong to one parent account (if not, it may be able to be modified for your department needs). It's very rudimentary, but it should work. I cannot guarantee it will work, so use it with caution.
# Because Canvas can only handle 100 results per page, perform a search on a second page and add to list of subaccounts if needed
$accountIds = (invoke-restmethod "https://yourCanvasInstance.instructure.com/api/v1/accounts/<parentAccountID>/sub_accounts?page=1&per_page=100" -method get -headers @{"Authorization" = "Bearer $auth"}).id
$accountIds += (invoke-restmethod "https://yourCanvasInstance.instructure.com/api/v1/accounts/<parentAccountID>/sub_accounts?page=2&per_page=100" -method get -headers @{"Authorization" = "Bearer $auth"}).id
$accountIds += '<parentAccountIfYouHaveOne>'
$accountIds
foreach ($a in $accountIds) {
$writeHeader = $true
$admins = (invoke-restmethod "https://yourCanvasInstance.instructure.com/api/v1/accounts/$a/admins?per_page=100" -method get -headers @{"Authorization" = "Bearer $auth"})
$accountName = invoke-restmethod "https://yourCanvasInstance.instructure.com/api/v1/accounts/$a" -method get -headers @{"Authorization" = "Bearer $auth"}
$literalAccountName = $accountName.name
$admins
Write-Host 'Admins for' $literalAccountName
if ($writeHeader) {
$header = "User's Name`tCanvas ID`tRole"
Add-Content "Account_Admins_$literalAccountName.txt" $header
$writeHeader = $false
}
foreach ($admin in $admins) {
$role = ($admin.role)
$line = [String]::Concat(($admin.user).name,"`t",($admin.user).id,"`t",$role)
$line
Add-Content "Account_Admins_$literalAccountName.txt" $line
}
}
** Edited to include the role for each user
Thanks, Parsa! Yes, this cycles through every subaccount. I'd like to know if it's possible to do this without making 100+ API requests.
True, unfortunately I have not been able to derive a solution that does this collectively in very few API calls. Fortunately it does not take too long to run. The problem with this example (as stated, it IS a rudimentary example) is if you had to run this (or any API script) constantly against your instance, then yes the number of API calls would have a more significant impact on performance.
The other issue is Canvas only supports (I think) the option to query the admins for a single subaccount at a time, so one would have to iteratively loop through an array of account IDs in order to return the admins for all subaccounts.
I agree with what llawson said, as far as documented APIs go, Canvas only supports querying the admins for a single sub account so you are stuck with looping through each sub account.
That being said, if you are looking for the sub accounts for a given user, you can reduce some of the resource demands by adding the user_id[] parameter and passing in the ID for that user. So, instead of needing to sort through a bunch of users you can replace the ?per_page=100 with ?user_id[]=##### and only get back the results for just that user.
Good luck!
Thanks, kenneth.larsen@usu.edu! That will be helpful.
I would just like to point out that I did not mark this answer as correct. Someone else did, and then again after I unmarked it as correct. I have unmarked it again. It is not the answer to my question.