CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • API: list contacts in group
Pages: [1]

Author Topic: API: list contacts in group  (Read 757 times)

kenahoo

  • I post occasionally
  • **
  • Posts: 83
  • Karma: 1
  • CiviCRM version: 4.1.1
  • CMS version: Drupal 7.15
  • MySQL version: 5.1.63
  • PHP version: 5.3.16
API: list contacts in group
January 08, 2014, 06:57:37 pm
I'm trying to find a way to use the API (OO version if possible) to list all the contacts in a certain group.  Using direct SQL, I would do something like this:

Code: [Select]
$result = db_query("
SELECT v.voice_part_4, c.display_name
FROM civicrm_contact c
JOIN civicrm_group_contact gc ON c.id=gc.contact_id
JOIN civicrm_group g ON gc.group_id=g.id
LEFT OUTER JOIN civicrm_value_constituent_information_1 v ON c.id=v.entity_id
WHERE gc.status='Added'
AND g.title='Current Singers'
AND c.is_deleted=0
ORDER BY FIND_IN_SET(SUBSTRING_INDEX(v.voice_part_4, ' ', 1), 'Soprano,Alto,Tenor,Bass'), v.voice_part_4, c.last_name
");

I'm not having luck so far, though.  Do I need to do an outer loop to get all the IDs for members of the group, then a fetch for each contact ID?  That seems much less efficient than the direct JOIN.  But I'm very new to the API, and I haven't found good documentation for it if there is any, so I may be missing a lot.

Thanks.

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: API: list contacts in group
January 08, 2014, 11:49:17 pm
You should use the GroupContact API? And use the API explorer and API doc to play? Have you found this chapter http://wiki.civicrm.org/confluence/display/CRMDOC/Using+the+API?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API: list contacts in group
January 09, 2014, 02:12:05 am
civicrm_api3("Contact","get", array(

Code: [Select]
$api = civicrm_api3 ();
$api->Contact->get (array("group_id"=>42));
print_r($api->values);

should do it
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

kenahoo

  • I post occasionally
  • **
  • Posts: 83
  • Karma: 1
  • CiviCRM version: 4.1.1
  • CMS version: Drupal 7.15
  • MySQL version: 5.1.63
  • PHP version: 5.3.16
Re: API: list contacts in group
January 09, 2014, 05:24:19 am
Xavier - I tried that, but it seems to list every contact in the database, ignoring the 'group_id' parameter.

kenahoo

  • I post occasionally
  • **
  • Posts: 83
  • Karma: 1
  • CiviCRM version: 4.1.1
  • CMS version: Drupal 7.15
  • MySQL version: 5.1.63
  • PHP version: 5.3.16
Re: API: list contacts in group
January 09, 2014, 05:27:58 am
Erik - so that means I'll have an outer loop to find contact_ids, and an inner fetch for each ID?  The following does work but it's pretty slow:

Code: [Select]
require_once 'sites/all/modules/civicrm/api/class.api.php';
$api = new civicrm_api3( array('conf_path' => 'sites/default') );



$params = array(
    'group_id' => 6,
    'sequential' => 1,
    'rowCount' => 100000,
);

if ($api->GroupContact->Get($params)) {
    $vals = array();
    $members = $api->values;
    foreach ($members as $gm) {
        $api->Contact->GetSingle(array('contact_id' => $gm->contact_id));
        $c = $api->value;
        $vals[] = array($c->display_name, $c->contact_id);
    }
    print_r($vals);
} else {
    echo "No results.";
}

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: API: list contacts in group
January 09, 2014, 08:09:50 am
Yep, that is what I would do. Do you need all fields from contact? If not, you got limit the amount of return fields to increase the speed a little as there is a lot of data in contact.
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

kenahoo

  • I post occasionally
  • **
  • Posts: 83
  • Karma: 1
  • CiviCRM version: 4.1.1
  • CMS version: Drupal 7.15
  • MySQL version: 5.1.63
  • PHP version: 5.3.16
Re: API: list contacts in group
January 09, 2014, 08:13:04 pm
I can limit the fields returned, but it doesn't make much difference.  I think most of the time is taken by so many round trips to the database.

The other challenge I haven't figured out yet is how to get the civicrm_value_constituent_information_1 information - is there an API for that stuff?  Is that an additional call to the database? 

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: API: list contacts in group
January 10, 2014, 02:34:36 am
You can use the API CustomGroup to find out the custom group and table name, CustomField to find out the related custom fields and CustomValue to retrieve or set the values.
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API: list contacts in group
January 11, 2014, 06:48:56 am
Hi,

It's easier/faster to api->Contact->Get (array("group"=>6, "return" => "sort_name,last_name"))

-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: API: list contacts in group
January 12, 2014, 02:13:27 am
Agree X, but I can not get groups using the Contact Get api either?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API: list contacts in group
January 12, 2014, 05:05:20 am
?

$api->Contact->Get (array ("return=>"sort_name,group,...")) returns the group,
 
array("group"=>42) filters

and you can combine the two. Not sure it was your question.
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: API: list contacts in group
January 12, 2014, 05:40:24 am
No it was not my question, but I tried anyway :-) And will use your solution in the coming days, so thanks!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • API: list contacts in group

This forum was archived on 2017-11-26.