Integrating C# app with Moodle 2

Moodle 2 exposes the same service methods using several protocols like XML-RPC, AMF and SOAP. In this post we’ll use XML-RPC, other protocols will be a subject of future posts. The examples below use existing Moodle 2 services. These are very few but I believe the Moodle 2 team will add more as time comes. In some future post I will discuss how you can add or extend such services.

Configure web services

To expose web service methods for Moodle 2 use the following documentation: Since this documentation is sparse we’ll list the basic steps required to configure a user that can access existing web service methods.

  1. Add a new Moodle user – we’ll assume this user will only access web service methods. Login as the Moodle admin and go to Site administration → Users → Accounts → Add a new user. Create a user with username wsuser and with the password Wsuser!23 (or any other username and password combination you please).
  2. Define a role that can be assigned to wsuser. Roles are how capabilities are assigned to users. Go to Site administration → Users → Permissions → Define roles. Add a new role. Call the role WebService. Assign the role to the Context type System (just tick the check box System).  Then, assign the following capabilities to the role:
    • webservice/xmlrpc:use
    • moodle/course:view
    • moodle/course:update
    • moodle/course:viewhiddencourses
    • moodle/user:viewdetails
  3. Assign the WebService role with all its associated capabilities to wsuser. Go to Site administration → Users → Permissions → Assign system roles. Select the WebService role. Assign wsuser to the role.
  4. Enable web services-  if they are already enabled skip this step. Go to Site administration → Advanced features. Tick the “Enable web services” check box. Save changes.
  5. Enable the xml-rpc protocol at Site administration → Plugins → Web services → Manage protocols.
  6. Create a new external service – Go to  Site administration → Plugins → Web services → External services. Add a new web service. Set the name to test, tick Enabled check box, and tick the “Authorised  user only” check box.
  7. Add the following functions to web service test:
    • moodle_course_get_courses
  8. Add user wsuser as authorized user of web service test. If the user has any capabilities missing, you will be prompted about missing capabilities. Add these capabilities as shown in step 2.

Create C# App

We’ll create an interface for the framework. Here’s the interface:

public interface IMoodle : IXmlRpcProxy
  object[] GetUserById(object[] id);
  object[] GetCourses();

Next, we call a method on this interface:

IMoodle moodleProxy;
moodleProxy = XmlRpcProxyGen.Create<IMoodle>();
moodleProxy.Url = "http://host/moodle/webservice/xmlrpc/simpleserver.php?wsusername=wsuser&wspassword=Wsuser!23";
object [] a = moodleProxy.GetCourses(); // returns an array of Hashtable

In the example above we recover a list of courses, the method GetCourses returns an array of Hashtable. Each hash table contains attributes of a course object. These are key value pairs that can be printed out using a simple method such as:

private string ConvertToString(object[] a)
  string ret = string.Empty;
  if (a.Count() <= 0 || !(a[0] is XmlRpcStruct) || !(a[0] is Hashtable))
    return ret;
  for (int i = 0; i < a.Count(); i++)
    Hashtable h = (Hashtable)a[i];
    IDictionaryEnumerator e = (IDictionaryEnumerator)h.GetEnumerator();
    while (e.MoveNext()) 
      ret += e.Key + "=" + e.Value + ",";
    ret += ";";
  return ret;

Here’s a sample output from this method:

timecreated=1282062417,summary=This is a test course,showreports=0,shortname=CF101,
fullname=Course Fullname 101,visible=1,groupmode=0,;

Next, we’ll see how we can recover a list of courses where each course is represented by an object.

Returning objects

Parameters and return values can be of a specific type. A Moodle course object can be represented by a struct such as:

public struct Course
  public int id;
  public string shortname;
  public int categoryid;
  public int categorysortorder;
  public string fullname;
  public string idnumber;
  public string summary;
  public int summaryformat;
  public string format;
  public int showgrades;
  public int newsitems;
  public int startdate;
  public int numsections;
  public int maxbytes;
  public int showreports;
  public int visible;
  public int hiddensections;
  public int groupmode;
  public int groupmodeforce;
  public int defaultgroupingid;
  public int timecreated;
  public int timemodified;
  public int enablecompletion;
  public int completionstartonenrol;
  public int completionnotify;
  public string lang;
  public string forcetheme;
  public override string ToString()
    string ret = string.Empty;
    FieldInfo [] attribs = this.GetType().GetFields();
    foreach (FieldInfo attrib in attribs)
      ret += attrib.Name + "=" + attrib.GetValue(this) + ",";
    return ret;

To return an array of Course objects, modify the GetCourses method in the IMoodle interface:

Course[] GetCourses();

Now, instead of a generic hash table for each object, we get a proper  Course object.

Authenticating using tokens

Tokens are hard to decipher values that can be used for authentication instead of username and password. Go to Site administration → Plugins → Web services → Manage tokens. Add a token for service test and user wsuser. This token can then be used in the url of the XML-RPC proxy:

moodleProxy.Url = "";

30 thoughts on “Integrating C# app with Moodle 2

  1. Hi,
    PLZ can you send me the source code ? ( as a project), really i find it very usful and helpful :)
    thnx very much :)

    1. Hi! I just realized how badly formatted the code was in the post. I have fixed it. I don’t have the original source code any more, unfortunately. If you or any one else happens to put it all together in a project please link to it. Thanks!

  2. “We’ll create an interface for the framework. ” …………. is it a project or C# project (console application)?!?!

  3. But when I click “add reference” and select “project”-tab, there is empty. How to select the project?
    And I can’t find the assembly(dll)…

      1. I’ve done that today, but I’m still waiting for a response :(

        If the webservices aren’t available there’s nothing that I could do, am I right?

        By the way, this was the only place where I actually saw an application that integrates C# with Moodle. Thanks.

  4. hello, plz help, where do I put this?

    IMoodle moodleProxy;
    moodleProxy = XmlRpcProxyGen.Create();
    moodleProxy.Url = “http://host/moodle/webservice/xmlrpc/simpleserver.php?wsusername=wsuser&wspassword=Wsuser!23”;
    object [] a = moodleProxy.GetCourses(); // returns an array of Hashtable

    do I create a method for it? or what?

  5. Thank alot. I got the courses that I created in my c# application.Please tell me how can i use other functions, like getting categories and creating users in moodle.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s