Web services

Moodle XML-RPC Web Service and C#

Picture of Barry Herbert
Moodle XML-RPC Web Service and C#

Hi folks

I'm new to the forum but not too Moodle and am stuck in a bit of a jam with my web service atm. I use Moodle to generate and populate a dynamic virtual world I've been creating for my PhD, with resources, activities etc

Previously I had been running an embedded Windows Form to show Moodle content but for my next phase i'm working on completely embedding all Moodle aspects into the virtual world to gamify the experience

My problem exists in getting well structured returns from the function calls

I'm using the following code to give an understanding of the framework:

To create my proxy connection

public interface IMoodle : IXmlRpcProxy

Course[] GetCourses();
Contents[] GetContents(int id);
Category[] GetCategories(object [] criteria);

My data manager:

private string token = "Empty";
public string localurl = "http://localhost/moodle/";
public string liveurl = "http://myliveipaddress/";
public string url;
IMoodle moodleProxy;
public Course[] courses;
public Category[] categories;
public Contents[] contents;

public DataManager(string token,bool live)
this.token = token;
url = liveurl;
url = localurl;

moodleProxy = XmlRpcProxyGen.Create<IMoodle>();
moodleProxy.Url = url+"webservice/xmlrpc/server.php?wstoken="+token;

public void LoadDataLibrary()
courses = moodleProxy.GetCourses();
categories = moodleProxy.GetCategories(new object[] {});
contents = moodleProxy.GetContents(courses[1].id);

My problem arises when it comes to the use of my structs to map the function results
The courses and categories work fine using these two structs:

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;


public struct Category
public int id;
public string name;
public string idnumber;
public string description;
public int descriptionformat;
public int parent;
public int sortorder;
public int coursecount;
public int visible;
public int visibleold;
public int timemodified;
public int depth;
public string path;
public string theme;

The issue is with my course contents function

I use this struct,which in turn calls the two others I will include:

public struct Contents
private int id;
private string name;
private int visible;
private string summary;
private int summaryformat;
private Module[] modules;

public struct Module
private int id;
private string url;
private string name;
private string description;
private int visible;
private string modicon;
private string modname;
private string modplural;
private int availablefrom;
private int availableuntil;
private int indent;
private ContentItem[] contents;

public struct ContentItem
private string type;
private string filename;
private string filepath;
private string filesize;
private string fileurl;
private string content;
private int timecreated;
private int timemodified;
private int sortorder;
private int userid;
private string author;
private string license;

When I debug the call to this function I get a set of three results however the only fields are that in the contents struct and the int properties are all 0 and string and more importantly array properties return with nulls

I have test content in under the courses but I cannot for the life of me figure out what is wrong with the manner in which its being converted as I've mirrored my structs exactly to what the Moodle API documentation details

Could any be kind enough to offer me any advise or assistance in figuring out exactly what I'm doing wrong here?

Many thanks and kind regards


Average of ratings: -