Calling "core_user_get_users" function using REST API

Calling "core_user_get_users" function using REST API

napisao/la Sonam Daultani -
Broj odgovora: 8

I am using Moodle Rest API in java to call core_user_get_users function and it is displaying the following error,
<EXCEPTION class="invalid_parameter_exception">
<ERRORCODE>invalidparameter</ERRORCODE>
<MESSAGE>Invalid parameter value detected</MESSAGE>
<DEBUGINFO>Missing required key in single structure: criteria</DEBUGINFO>
</EXCEPTION>

I tried to give criteria[0]=email, but it gave me below error,

criteria => Invalid parameter value detected: Only arrays accepted. The bad value is: 'email'


It would be highly appreciated if someone could help me in how to pass the criteria parameter ?

U odgovoru na Sonam Daultani

Re: Calling "core_user_get_users" function using REST API

napisao/la Harout Bezdjian -

Hi Sonam,

I had the similar problem but I solved it.

I used the curl function in PHP page, the problem was the criteria[0] takes an array which has 'key' as a single structure and 'value' as a multiple structure and it should be like the following (it's in PHP),

 

In my case i'm checking the user by its username.

$username = array( 'key' => 'username' ,  'value' => 'theUserName' );
$params = array( 'criteria' => array($username));

$server_url = $domain_name . '/webservice/rest/server.php' . '?wstoken=' . $token . '&wsfunction=' . $function_name;
$curl = new curl;
$rest_format = ($rest_format == 'json') ? '&moodlewsrestformat=' . $rest_format : '';

$resp = $curl->post($server_url . $rest_format, $params);

It worked for me in PHP, I'm sure in Java you can write a similar code.

Good luck smešak

 

U odgovoru na Sonam Daultani

Re: Calling "core_user_get_users" function using REST API

napisao/la Bill Antonia -

You have to pass the parameters as:

.....&criteria[0][key]=email&criteria[0][value]=aname@somedomain

With the ..... being the rest of the URL. Note [key] and [value] are an integral part of the call, they don't get dropped or replaced.

If you want to add more criteria then add:

.....&criteria[1][key]=email&criteria[1][value]=anothername@somedomain

 Note you also have to URL encode the string. I'm currently updating my Java library, MoodleRest, which you can download the source from GitHub (If you do, download on a regular basis as it is being updated very frequently as I am trying to catch up, it was a little out of date!)

U odgovoru na Bill Antonia

Re: Calling "core_user_get_users" function using REST API

napisao/la Richard Chui -

If I use "id" as key, I can get all the other users in Moodle.  But if I use any other field as key (e.g. username, email, etc), I can only get the user who was assigned the webservice role.  Any idea why?

 

Thanks a lot!

U odgovoru na Richard Chui

Re: Calling "core_user_get_users" function using REST API

napisao/la Richard Chui -

It seems that since username is not in $userfields, it is not set and returned in function user_get_user_details() in /user/lib.php.  And when it is not set, the user is considered to be invalid in function get_users() in /user/externallib.php.  The fields returned must contain the criteria field for this user to be considered valid.

U odgovoru na Richard Chui

Re: Calling "core_user_get_users" function using REST API

napisao/la Andre Figueiredo -

Hi Richard!

I have the same problem...when I use key=id, the core_user_get_users(and _by_field too) returns all users or a specific user, but when I use key=something else, doesn't work.

 And when I list all user, the email address and username does not appear in response...

How do you resolve this?

Thanks!

U odgovoru na Bill Antonia

Re: Calling "core_user_get_users" function using REST API

napisao/la Julian Corrêa -

Hi Bill.


I need to look for IDNumber or username, but I can not.
   See the example:

http://172.29.6.9:8080/webservice/rest/server.php?wstoken=3e5e578d10544df1019d06cf48938e1f&wsfunction=core_user_get_users&moodlewsrestformat=json&criteria[0][key]=idnumber&criteria[0][value]=11221515

But returns nothing.

It seems to me that only the fields are valid for this service.

      id
      firstname
      lastname
      fullname
      email
      address
      phone1
      phone2
      firstaccess
      lastaccess
      city
      country
      profileimageurlsmall
      profileimageurl
 
Have tried using core_user_get_users_by_field service, but without success.
Example: http://172.29.6.9:8080/webservice/rest/server.php?wstoken=3e5e578d10544df1019d06cf48938e1f&wsfunction=core_user_get_users_by_field&moodlewsrestformat=json&field=username&values[0]=2711221515

Can you help me? Someone?

U odgovoru na Julian Corrêa

Re: Calling "core_user_get_users" function using REST API

napisao/la Bill Antonia -

Here is an example:

http://www.domain.com/moodle/webservice/rest/server.php?wstoken=averylongstringforatoken&wsfunction=core_user_get_users_by_field&field=email&values[0]=a.name@some.domain.com

Replace the field name and the value as appropriate but keep it as an array ie for another person add &values[1]=a.notherperson@some.domain.com to the above to return two results.

In your example you are searching on the field username but you are using a numeric for the search string. Your first example has the incorrect format however in the second the format is correct.

Not sure about the conversion to Json though, my library creates Java objects from the returned XML data.

Regards

Bill

U odgovoru na Bill Antonia

Re: Calling "core_user_get_users" function using REST API

napisao/la Julian Corrêa -

Bill, in my database username field is populated only with numbers because it comes from another system.
The fact that the data in the field is numeric USERNAME has nothing to do with the error that I reported.

Found it is a permission problem. The user WebService has to be the site administrator.

See https://tracker.moodle.org/browse/MDL-42639.

Tks