Skip to content

Instantly share code, notes, and snippets.

@fghber
Last active November 10, 2022 08:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fghber/e54d748f1c5bed0b6e4cb1e3da57471d to your computer and use it in GitHub Desktop.
Save fghber/e54d748f1c5bed0b6e4cb1e3da57471d to your computer and use it in GitHub Desktop.
Create User with Moodle Rest API
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;
using System.Web;
namespace MoodleRestTest
{
class Program
{
internal static string _token = "";
static void Main(string[] args)
{
MoodleUser user = new MoodleUser();
user.username = HttpUtility.UrlEncode("dorwin@fakeemail.com");
user.password = HttpUtility.UrlEncode("Pass@Fakew0rd");
user.firstname = HttpUtility.UrlEncode("Daryl");
user.lastname = HttpUtility.UrlEncode("Orwin");
user.email = HttpUtility.UrlEncode("dorwin@fakeemail.com");
string postData = string.Format("users[0][username]={0}&users[0][password]={1}&users[0][firstname]={2}&users[0][lastname]={3}&users[0][email]={4}", user.username, user.password, user.firstname, user.lastname, user.email);
HttpStatusCode httpStatusCode;
var contents = RestCall("core_user_create_users", postData, out httpStatusCode);
// Deserialize
JavaScriptSerializer serializer = new JavaScriptSerializer();
if (contents.Contains("exception"))
{
// Error
MoodleException moodleError = serializer.Deserialize<MoodleException>(contents);
}
else
{
// Good
List<MoodleCreateUserResponse> newUsers = serializer.Deserialize<List<MoodleCreateUserResponse>>(contents);
}
}
private static string RestCall(string serviceType, string postData, out HttpStatusCode statusCode)
{
string requestUrl = string.Format("https://apoudev.mrooms3.net/webservice/rest/server.php?wstoken={0}&wsfunction={1}&moodlewsrestformat=json", _token, serviceType);
// Call Moodle REST Service
HttpWebRequest req = (HttpWebRequest) WebRequest.Create(requestUrl);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
// Encode the parameters as form data:
byte[] formData = Encoding.UTF8.GetBytes(postData);
req.ContentLength = formData.Length;
// Write out the form Data to the request:
using (Stream post = req.GetRequestStream())
{
post.Write(formData, 0, formData.Length);
}
// Get the Response
using (var resp = (HttpWebResponse) req.GetResponse())
{
statusCode = resp.StatusCode;
using (var resStream = resp.GetResponseStream())
{
//if (resStream != null)
using (var reader = new StreamReader(resStream)) //will throm if resStream is null
{
return reader.ReadToEnd();
}
}
}
}
public class MoodleUser
{
public string username { get; set; }
public string password { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public string email { get; set; }
}
public class MoodleException
{
public string exception { get; set; }
public string errorcode { get; set; }
public string message { get; set; }
public string debuginfo { get; set; }
}
public class MoodleCreateUserResponse
{
public string id { get; set; }
public string username { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment