Here is the code that worked:
*****************************************************************************************************************************************************
Start of Powershell Script
*****************************************************************************************************************************************************
# Script to copy course content from one existing course to another
# Version 0.1
#Script Requires PowerShell 3 or higher
# Input File Format is a CSV file with the following headers:
# source_id,destination_id
# Configuration Variables
$canvasURL = 'rsccd.test.instructure.com' # Canvas Instance URL do not include https:// (Example: canvas.instructure.com)
(this line was edited to remove token)
$inputFile = 'c:\canvas\copycourses.csv' # Full Path to file to process
$useSISIDs = $true # Set to $true if your input file provides SIS IDs for both source and destination course IDs. Set to $false if your input file uses Canvas IDs for both source and destination course IDs.
#************ Do Not Edit Below This Line ************
#Check for file
if(!(Test-Path $inputFile)){
Write-Host Input file does not exist!
exit
}
$headers = @{"Authorization"="Bearer "+$token}
$in_data = Import-CSV($inputFile);
if($useSISIDs){
$idPrefix = "sis_course_id:"
}else{
$idPrefix = ""
}
forEach($item in $in_data){
$fromCourse = $idPrefix + $item.source_id
$toCourse = $idPrefix + $item.destination_id
$url = 'https://'+$canvasURL+'/api/v1/courses/'+$toCourse+'/content_migrations'
Write-Host $url
$postData = @{'migration_type'='course_copy_importer'; 'settings[source_course_id]'=$fromCourse}
Write-Host Starting copy of course $fromCourse to course $toCourse
Try {
$results = (Invoke-WebRequest -Headers $headers -Method POST -Uri $url -Body $postData)
Try{
$jresults = ($results.Content | ConvertFrom-Json)
if($jresults.id){
Write-Host " Started"
}
}Catch{
Write-Host " Unable to complete request. An error occured: " + $_.Exception
}
} Catch {
$errorMsg = $_.Exception
#$errorMsg
$terminate = $false
if($errorMsg.Response.StatusCode -eq 'unauthorized'){
$msg = "The provided token is not from a user with sufficient permission to complete the action."
$terminate = $true
}elseif($errorMsg.Response.StatusCode.Value__ -eq "400"){
$msg = "Unable to start copy."
$terminate = $false
}else{
$msg = "An error occured: " + $errorMsg
$terminate = $true
}
Write-Host " ERROR:" $msg
if($terminate){ break }
}
Write-Host Done
}
*****************************************************************************************************************************************************
End of Powershell Script
*****************************************************************************************************************************************************
I wanted to copy the course template from course named "TC Template 1" to course named "SAC Sandbox" (empty course shell imported via SIS import).
I noticed the "SAC Sandbox" had a SIS ID but the "TC Template 1" did not have a SIS ID filled out in Canvas. (You can see it when you go into the course and click on Settings )
The script did not work even when I changed $useSISIDs to false .
Once I added a SIS ID ( "TCTemp1" ) for the "TC Template 1" course- this is what the courses looked like in Canvas.
TC Template 1 with SIS ID TCTemp1
SAC Sandbox with SAC-Sandbox
For testing the script with one course, I had my copycourses.csv filled with only one course:
This is what the csv file looked like:
source_id, destination_id
TCTemp1, SAC-Sandbox
I changed the Powershell Script set $useSISIDs to true and it worked.
For multiple courses, the only change I had to do was create a csv file with all courses.
This is a sample:
here I have one template copied to several course shells created by SIS import.
source_id, destination_id
TCTemp1, SAC-Sandbox
TCTemp1, course1
TCTemp1, course2
TCTemp1, course3
TCTemp1, course4
TCTemp1, course5
TCTemp1, course6
TCTemp1, course7
TCTemp1, course8
TCTemp1, course9
TCTemp1, course10
(In this file the destination_id column is populated by are SIS IDs of course shells ).
Let me know if there are questions.
Thank you,
Asha
This discussion post is outdated and has been archived. Please use the Community question forums and official documentation for the most current and accurate information.