5

Starting from a contact ID, i need to find all the groups to which the contact belongs.

I know that i can use api GroupContact but it doesn't work for smart groups (in CiviCRM 4.5)

Is there any api calls that will retrieve all groups at once ? Otherwise, what would be the best way ?

6

To get the Groups, you can use the GroupContact API

$result = civicrm_api3('GroupContact', 'get', array(
  'sequential' => 1,
  'contact_id' => <contact_id>,
));

To get all the smart groups to which the contact belongs, you can use this BAO function defined in GroupContactCache.php

$smartGroup = CRM_Contact_BAO_GroupContactCache::contactGroup($contactId);

returned $smartGroup will be the list of smart_groups for the contactId passed

  • Thanks @Jitendra. Does that mean that you have tested that the GroupContact API does not return Smart Groups? – ErikH - CiviCooP Sep 1 '15 at 7:45
  • yes, civicrm_group_contact does not have an entry for smart groups with status Added as it does other normal groups. – jitendra - fuzion Sep 1 '15 at 8:36
  • Thanks Jitendra Purohit. It's a shame there is no function to get all at once. – samuelsov Sep 1 '15 at 14:19
0

If you mean the api call "civicrm_api3_group_contact_get($params)" where you are passing contact_id, I think it will return all groups, whether 'Smart' or not - did you try it? That call just passes contact_id and if status is 'added' it should return the group

  • 1
    Thanks for your answer but no, it doesn't. civicrm_api3_group_contact_get is linked to the civicrm table civicrm_group_contact which is only for member statically added to group. – samuelsov Sep 1 '15 at 14:16
0

I am almost sure that the API GroupContact also returns Smart Group membership, my recommendation would be to test.

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.