Enrol User to Course

Enrol User to Course

par Bhagya Thilakarathne,
Nombre de réponses : 1

Error Message
{"exception":"invalid_parameter_exception","errorcode":"invalidparameter","message":"Invalid parameter value detected","debuginfo":"Context does not exist"}

My Code

 public class MoodleEnrollment
    {

        public int roleId { get; set; }

        public int userId { get; set; }

        public int courseId { get; set; }

        public int? timestart { get; set; }

        public int? timeend { get; set; }

        public int? suspend { 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; }
    }

 private void btnEnrollUser_Click(object sender, EventArgs e)
        {
            try
            {
                String token = @"7d95537c192a6b7dad063abe3423a06e";
                string moodleDomainName = @"https://moodle.stg.evolution.edu.au";

                MoodleEnrollment enrollment = new MoodleEnrollment()
                {
                    roleId = 5, // student role id
                    courseId = 48, // course id
                    userId = 136, // username
                    timestart = 0,
                    timeend = 0,
                    suspend = 0
                };

                // string enrolData = string.Format("enrolments[0][roleid]={0}&enrolments[0][userid]={1}&enrolments[0][courseid]={2}&enrolments[0][timestart]={3}&enrolments[0][timeend]={4}&enrolments[0][suspend]={5}", enrollment.roleId, enrollment.userId, enrollment.courseId, enrollment.timestart, enrollment.timeend, enrollment.suspend);
                String enrolData = String.Format("enrolments[0][roleid]={0}&enrolments[0][userid]={1}&enrolments[0][courseid]={2}", enrollment.roleId, enrollment.userId, enrollment.courseId);
                string createRequest = string.Format(moodleDomainName + "/webservice/rest/server.php?wstoken={0}&wsfunction={1}&moodlewsrestformat=json", token, "enrol_manual_enrol_users");

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(createRequest);
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";

                byte[] formData =
                    UTF8Encoding.UTF8.GetBytes(enrolData);  //or use "userData"
                req.ContentLength = formData.Length;
                // Write out the form Data to the request:
                using (Stream post = req.GetRequestStream())
                {
                    post.Write(formData, 0, (int)req.ContentLength);
                }
                // Get the Response
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                Stream resStream = resp.GetResponseStream();
                StreamReader reader = new StreamReader(resStream);
                string contents = reader.ReadToEnd();

                // Deserialize
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                if (contents.Contains("exception"))
                {
                    MoodleException moodleError = serializer.Deserialize<MoodleException>(contents);
                }
                else
                {
                    List<MoodleCreateUserResponse> newUsers = serializer.Deserialize<List<MoodleCreateUserResponse>>(contents);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Is there anyone can help me? 

Moyenne des évaluations  -