1

We've created so called 'smart' groups in civicrm, and we need to know if a certrain user belongs to this group.

I can get the groups of a user with the following code:

http://url.com/sites/all/modules/civicrm/extern/rest.php?debug=0&entity=GroupContact&action=get&contact_id='.$uid.'&json={"sequential":1}&api_key='.$api_key.'&key='.$site_key

But the "Smart group of the UID is not listed here,. When I do the following:

http://url.com/sites/all/modules/civicrm/extern/rest.php?debug=0&entity=Contact&action=get&group='.$gid.'&version=3&contact_id='.$uid.'&json={"sequential":1}&api_key='.$api_key.'&key='.$site_key

I get the result, that the user belongs to this group.

What I want is only to give the contact_id as parameter to check in which smart groups this user is

1

Smart Groups are basically a saved search. Checking whether a given contact is in a saved search result means running the search for that combination, which is not efficient - hence the reason the information you want isn't available through existing APIs.

See eg CRM-11903: GroupContact API doesn't get smart group members.

If you want to be able to query the API for this information, you have some options -

  • As you're doing, use the Contact API and filter by group ID to check each Contact/Smart group pair. Not efficient, but you've already found it works.
  • Create a "regular" group and automate the process of updating its members based on the criteria of your smart group. A bit rough - but probably not too hard.
  • Implement the functionality you need in a new API method. Probably not as hard as it sounds, check existing API methods for examples.
  • While it can't be relied on, it might help you to know there's also a DB table which caches the mapping of contacts to groups incl smart groups: civicrm_group_contact_cache - just remember it's a cache table so not to be trusted! – Chris Burgess Mar 26 '16 at 10:37

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.