RosterServer: .NET

⚠️

Exciting News!

Our transition to a new website is complete. Click here to seamlessly access the latest version of this article.

Kindly update any bookmarked URLs accordingly. The Development Center will no longer be accessible after April 1, 2024. Thank you for your attention to this matter.

📘

GitHub and DLL download links

.NET Core GitHub
Download .NET Core DLL

.NET Framework GitHub
Download .NET Framework DLL

Setup

To use RosterServer, you need to download the DLL and add a reference to it in your project. To do this in Visual Studio 2017, right click Dependencies -> Add References -> Browse -> Select DLL -> Add.

Usage

First, you need to import ClassLink.OneRoster.

using ClassLink.OneRoster;

Next, create a new instance of the OneRoster object, which takes in two strings,
the client_id and client_secret.

OneRoster oneRoster = new OneRoster("XXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXX");

You are now able to make requests to a specified URL. This is done using the method MakeRosterRequest(url), which takes in the URL and returns a OneRosterResponse object containing the status code and the JSON response.

OneRoster oneRoster = new OneRoster("XXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXX");
OneRosterResponse response = oneRoster.MakeRosterRequest("https://example.com/users");

The response is made up of StatusCode, which contains the status code as an int, and Response, which contains the JSON response as a string.

Console.WriteLine(response.StatusCode); // The status code
Console.WriteLine(response.Response); // The JSON response

Example

📘

Example

Print all students' names

using System;
using System.Linq;
using Newtonsoft.Json.Linq;
// import OneRoster
using ClassLink.OneRoster;

namespace Example
{
    class Program
    {
        static JToken[] getUsers(string url)
        {
            // Create new OneRoster object with the client_id and client_secret
            OneRoster oneRoster = new OneRoster("XXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXX");
            
            // Make the request to the given url
            OneRosterResponse response = oneRoster.MakeRosterRequest(url);
            
            // Parse response
            JObject parsed = JObject.Parse(response.Response);
            
            // If status_code is 200, create array of users from response, otherwise print message and return null
            switch (response.StatusCode)
            {
                case 200:
                    return parsed.Property("users").Value.ToArray();
                case 401:
                    Console.WriteLine("Unauthorized Request\n" + parsed.Property("code") + response.Response);
                    break;
                case 404: 
                    Console.WriteLine("Not Found\n" + parsed.Property("code") + response.Response);
                    break;
                case 500:
                    Console.WriteLine("Server Error\n" + parsed.Property("code") + response.Response);
                    break;
                default:
                    Console.WriteLine("Something Went Wrong, status code " + response.StatusCode + "\n" 
                                      + response.Response);
                    break;
            }

            return null;
        }

        static void Main(string[] args)
        {
            JToken[] users =
                getUsers(
                    "https://example.com/users?fields=givenName%2CfamilyName&filter=role%3D'student'&limit=10&offset=50&orderBy=asc");
            if (users != null)
            {
                foreach (var user in users)
                {
                    Console.WriteLine(user["givenName"] + " " + user["familyName"]);
                }
            }
        }
    }
}