3

While trying this request

$result = civicrm_api3('Contact', 'get', array(   
'return' => "id",   
'group' => "Administrators", ));

we've got

{
    "error_code": "no such field",
    "sql": "SELECT contact_a.id as contact_id, contact_a.is_deleted  as `contact_is_deleted`, `civicrm_group_contact-Administrators`.id as group_contact_id, `civicrm_group_contact-Administrators`.status as status  FROM civicrm_contact contact_a  LEFT JOIN civicrm_group_contact `civicrm_group_contact-Administrators` ON (contact_a.id = `civicrm_group_contact-Administrators`.contact_id AND `civicrm_group_contact-Administrators`.status IN (\"Added\"))   LEFT JOIN civicrm_group_contact_cache `civicrm_group_contact_cache_Administrators` ON contact_a.id = `civicrm_group_contact_cache_Administrators`.contact_id   WHERE  ( contact_a.is_deleted = '0' AND ( ( `civicrm_group_contact-Administrators`.group_id IN ( Administrators ) ) OR ( `civicrm_group_contact_cache_Administrators`.group_id IN (\"Administrators\") ) ) )  AND  ( 1 )  AND  ( 1 )   GROUP BY contact_a.id LIMIT 0, 25  [nativecode=1054 ** Unknown column 'Administrators' in 'where clause']",

whatever operator ( "=" , "Like", "In" ) is being used.

All we wanted is Contact IDs, which should be passed to another API call. Of course, we can get data by Search in Group in browser and export all values, when read csv file and extract IDs, but it would be much prettier with pure API calls.

3 Answers 3

4

You should then use another entity; GroupContact. Remember to add the Status=Added

1
  • 1
    This works like a charm, thanks! $result = civicrm_api3('GroupContact', 'get', array( 'sequential' => 1, 'return' => "contact_id", 'group_id' => "Administrators", ));
    – yurg
    Oct 19, 2015 at 23:38
3

I think you should target the Group entity and chain it with Group Contact like this:

$result = civicrm_api3('Group', 'get', array( 
'sequential' => 1,
'name' => "Administrators",
'api.GroupContact.get' => array('status' => "Added"),
));

The GroupContact will return the contact_id

3

Andrei and Flying answer seems good to me but if i look at civicrm api examples in the core code, you should be able to do this :

$result = civicrm_api3('Contact', 'get', array(   
  'return' => "id",   
  'filter.group_id' => array(0 => 1)
));

as found in : https://github.com/civicrm/civicrm-core/blob/master/api/v3/examples/Contact/GroupFilterUsingContactAPI.php

EDIT 2019-05-31: the API seems to have changed. Now it's :

$result = civicrm_api3('Contact', 'get', array(   
  'return' => "id",   
  'group' => [
    'IN' => [0 => 1]
  ],
));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.