AnsweredAssumed Answered

Rest API C#, RestSharp, and Newtonsoft Enrollments

Question asked by James Featherstone on Sep 24, 2019
Latest reply on Sep 24, 2019 by Robert Carroll

I am trying to use the API using C#, RestSharp, and Newtonsoft. I am trying to GET enrollments by Section Id. I have created classes to handle the JSON that is returned, but I am getting errors. The classes are listed below.

[JsonObject]
public class Grades
{
   public string html_url { get; set; }
   public object current_grade { get; set; }
   public object current_score { get; set; }
   public object final_grade { get; set; }
   public double final_score { get; set; }
   public object unposted_current_score { get; set; }
   public object unposted_current_grade { get; set; }
   public double unposted_final_score { get; set; }
   public object unposted_final_grade { get; set; }
}
[JsonObject]
public class User
{
   public int id { get; set; }
   public string name { get; set; }
   public DateTime created_at { get; set; }
   public string sortable_name { get; set; }
   public string short_name { get; set; }
   public object sis_user_id { get; set; }
   public object integration_id { get; set; }
   public object sis_import_id { get; set; }
   public string login_id { get; set; }
}
[JsonObject]
public class Enrollment
{
   public int id { get; set; }
   public int user_id { get; set; }
   public int course_id { get; set; }
   public string type { get; set; }
   public DateTime created_at { get; set; }
   public DateTime updated_at { get; set; }
   public object associated_user_id { get; set; }
   public object start_at { get; set; }
   public object end_at { get; set; }
   public int course_section_id { get; set; }
   public int root_account_id { get; set; }
   public bool limit_privileges_to_course_section { get; set; }
   public string enrollment_state { get; set; }
   public string role { get; set; }
   public int role_id { get; set; }
   public object last_activity_at { get; set; }
   public object last_attended_at { get; set; }
   public int total_activity_time { get; set; }
   public object sis_import_id { get; set; }
   public Grades grades { get; set; }
   public string sis_account_id { get; set; }
   public string sis_course_id { get; set; }
   public object course_integration_id { get; set; }
   public string sis_section_id { get; set; }
   public object section_integration_id { get; set; }
   public object sis_user_id { get; set; }
   public string html_url { get; set; }
   public User user { get; set; }
}

public class RootObject
{
   public List<Enrollment> enrollList { get; set; }
}

 

Below is the the function that is called to get the list of enrollments.

public RootObject getListSectionEnrollments(string baseUrl, string section_id, string authToken, string[] type = null, string[] role = null, string[] state = null )
{
   type = type ?? new string[] { "StudentEnrollment", "TeacherEnrollment", "TaEnrollment", "DesignerEnrollment", "ObserverEnrollment" };
   role = role ?? new string[] { "StudentEnrollment", "TeacherEnrollment", "TaEnrollment", "DesignerEnrollment", "ObserverEnrollment" };
   state = state ?? new string[] { "active", "invited" };

   string apiCall = "/api/v1/sections/:section_id/enrollments".Replace(":section_id", section_id);
   string url = baseUrl + apiCall;
   var client = new RestClient(url);

   var request = new RestRequest(Method.GET);

   request.AddHeader("Authorization", "Bearer " + authToken);
   request.AddParameter("type[]", type);
   request.AddParameter("role[]", role);
   request.AddParameter("state[]", state);

   request.RequestFormat = DataFormat.Json;
   var response = client.Execute(request);

   RootObject myObject = JsonConvert.DeserializeObject<RootObject>(response.Content);
   return myObject;
}

All the above code is in a public class called Enrollments. Which is then built into a dll that I use in my C# programs. The program calls the function like this. The second line is meant as a test to ensure that there is something in the enrollments object.

Enrollments.RootObject enrollments = en.getListSectionEnrollments(ConStrings.canvasLiveBaseUrl, canvas_id, ConStrings.canvasProdToken, new string[] { "StudentEnrollment"});   
MessageBox.Show(enrollments.enrollList.Count.ToString());                                          

The second line above causes the below error.

Object reference not set to an instance of an object.System.NullReferenceException: Object reference not set to an instance of an object.

Outcomes