4

Given the ID of a smart group, what API/BAO/whatever do I need to use to retrieve all contacts belonging to that group? I've been looking through the source code all morning and can't seem to find what I need. Thanks.

2 Answers 2

3

As of version 4.6, you can use the API Explorer (top menu: Support > Developer > API Explorer) to get code, examples and docs. You can use the entities and actions to find the right combination to get the info you're looking for. (You can use any number of actions, but it is a live API tool so if you e.g. update an entity, it will change values in the database).

In this case, go to the Explorer then click the tab for Examples. Choose the entity: Contact and then example: GroupFilterUsingContactAPI. That generates this code (taken from the Drupal Demo at dmaster.demo.civicrm.org, but the API explorer is not CMS dependent):

<?php
/**
 * Test Generated example demonstrating the Contact.get API.
 *
 * Get all from group and display contacts.
 *
 * @return array
 *   API result array
 */
function contact_get_example() {
  $params = array(
    'filter.group_id' => array(
      '0' => 1,
      '1' => 26,
    ),
    'contact_type' => 'Individual',
  );

  try{
    $result = civicrm_api3('Contact', 'get', $params);
  }
  catch (CiviCRM_API3_Exception $e) {
    // Handle error here.
    $errorMessage = $e->getMessage();
    $errorCode = $e->getErrorCode();
    $errorData = $e->getExtraParams();
    return array(
      'error' => $errorMessage,
      'error_code' => $errorCode,
      'error_data' => $errorData,
    );
  }

  return $result;
}

/**
 * Function returns array of result expected from previous function.
 *
 * @return array
 *   API result array
 */
function contact_get_expectedresult() {

  $expectedResult = array(
    'is_error' => 0,
    'version' => 3,
    'count' => 1,
    'id' => 1,
    'values' => array(
      '1' => array(
        'contact_id' => '1',
        'contact_type' => 'Individual',
        'contact_sub_type' => '',
        'sort_name' => '[email protected]',
        'display_name' => '[email protected]',
        'do_not_email' => 0,
        'do_not_phone' => 0,
        'do_not_mail' => 0,
        'do_not_sms' => 0,
        'do_not_trade' => 0,
        'is_opt_out' => 0,
        'legal_identifier' => '',
        'external_identifier' => '',
        'nick_name' => '',
        'legal_name' => '',
        'image_URL' => '',
        'preferred_communication_method' => '',
        'preferred_language' => 'en_US',
        'preferred_mail_format' => 'Both',
        'first_name' => '',
        'middle_name' => '',
        'last_name' => '',
        'prefix_id' => '',
        'suffix_id' => '',
        'formal_title' => '',
        'communication_style_id' => '',
        'job_title' => '',
        'gender_id' => '',
        'birth_date' => '',
        'is_deceased' => 0,
        'deceased_date' => '',
        'household_name' => '',
        'organization_name' => '',
        'sic_code' => '',
        'contact_is_deleted' => 0,
        'current_employer' => '',
        'address_id' => '2',
        'street_address' => '1 my road',
        'supplemental_address_1' => '',
        'supplemental_address_2' => '',
        'city' => '',
        'postal_code_suffix' => '',
        'postal_code' => '',
        'geo_code_1' => '',
        'geo_code_2' => '',
        'state_province_id' => '',
        'country_id' => '',
        'phone_id' => '',
        'phone_type_id' => '',
        'phone' => '',
        'email_id' => '1',
        'email' => '[email protected]',
        'on_hold' => 0,
        'im_id' => '',
        'provider_id' => '',
        'im' => '',
        'worldregion_id' => '',
        'world_region' => '',
        'individual_prefix' => '',
        'individual_suffix' => '',
        'communication_style' => '',
        'gender' => '',
        'state_province_name' => '',
        'state_province' => '',
        'country' => '',
        'id' => '1',
      ),
    ),
  );

  return $expectedResult;
}

/*
* This example has been generated from the API test suite.
* The test that created it is called "testGetGroupIDFromContact"
* and can be found at:
* https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/ContactTest.php
*
* You can see the outcome of the API tests at
* https://test.civicrm.org/job/CiviCRM-master-git/
*
* To Learn about the API read
* http://wiki.civicrm.org/confluence/display/CRMDOC/Using+the+API
*
* Browse the api on your own site with the api explorer
* http://MYSITE.ORG/path/to/civicrm/api
*
* Read more about testing here
* http://wiki.civicrm.org/confluence/display/CRM/Testing
*
* API Standards documentation:
* http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards
*/
0
2

There is also a "less-code" variation of this:

  $result = civicrm_api3('Contact', 'get', array(
        'group' => array('IN' => array(1,2,3)), // <-- Group IDs array
    ));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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