cancel
Showing results for 
Search instead for 
Did you mean: 

Canvas Calendar Institutional Event Creator

chadscott
Community Contributor
2 0 167

Purpose:

The purpose of this script is to add institutional calendar events to users' personal Canvas calendars.
For example, we are going to add grading period start/end dates, district holidays, and testing information to Teacher and Student Calendars. 

eventexample1.png

 

 

 

 

# import gems
require 'typhoeus'
require 'json'
require 'csv'

canvas_url = 'https://XXXX.instructure.com' # put full canvas prod/test url eg: https://school.instructure.com
canvas_token = 'XXXXXXXXX' # put canvas API token here
csv_file1 = 'C:\XXXX\users.csv' #full path to users.csv
csv_file2 = 'C:\XXXX\calendar_events.csv' #full path to calendar_events.csv

start_timer = Time.now

CSV.foreach(csv_file1, headers: true) do |row|
    user_id = row['id']

    CSV.foreach(csv_file2, headers: true) do |row2|
        calendar_title = row2['title']
        calendar_date = row2['start']

        update_calendar = Typhoeus::Request.new(
            "#{canvas_url}/api/v1/calendar_events?as_user_id=#{user_id}",
            method: :post,
            headers: { authorization: "Bearer #{canvas_token}" },
            params: {
                "calendar_event[context_code]" => "user_#{user_id}",
                "calendar_event[title]" => "#{calendar_title}",
                "calendar_event[all_day]" => true,
                "calendar_event[start_at]" => "#{calendar_date}",
                "calendar_event[time_zone_edited]" => "America/Chicago"
            }
            )
        update_calendar.run
    end
end

finish_timer = Time.now
diff = finish_timer - start_timer
puts "Script done running! Took #{diff} seconds to run."

 

 

 

 

 

 

Instructions:

Install Ruby and the following gems - typhoeus, json, and csv.

  1. Copy the script above and create a file in your desired location.
  2. Create calendar_events CSV file and add events.
    1. Column 1 is 'title' of event
    2. Column 2 is 'start date' of event (YYYYMMDD format)
  3. Save file to the same folder as the script file.
    calendarexample1.png
  4. Run Provisioning report USERS for term. (We will do this twice as our elementary and secondary terms differ)
  5. Filter csv file for student and teacher users only.
  6. Copy/paste results into new CSV file.
  7. Save file as users.csv in the same folder as the script file.
  8. Remove all columns except Canvas ID column, rename to "id".
  9. Save file.
    usersexample1.png
  10. Update script above with Canvas URL, Canvas Token, file paths for csv_file1 and csv_file2.
  11. Run the script file.

These will be all day events. You could change these to be specific dates by adding time to the start date and then add a new line for end date to the script. The format for a datetime is YYYYMMDDTHHMMSSZ (Example - 20210405T063000Z). If you do add time, you'll need to update your timezone, otherwise it doesn't really matter for all day events when only using the date format.

Limitations:

I've not tested this on a full term yet as we will be doing it for the first time this summer as a trial and then will deploy fully in the Fall. If you need to run more than once (new users, etc.), I would diff the user file and only run against new users, otherwise you will duplicate events on everyone's calendar.

I can not support this script for you. If you do not know how to install/run a Ruby script, please utilize resources found here and on Google to help with that process.

Future:

I would like to add a checker to determine if an event with that title is already on that day and if so, skip it. If you have ideas of how to do this, please contribute and share!

Please feel free to iterate and make this better and modify for your institutional goals.

Cheers,

Chad Scott
LMS Support Specialist
Katy ISD Instructional Technology