Answers.Body: <p>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.</p>
  
  <p>See eg <a href="https://issues.civicrm.org/jira/browse/CRM-11903" rel="nofollow">CRM-11903: GroupContact API doesn't get smart group members</a>.</p>
  
  <p>If you want to be able to query the API for this information, you have some options -</p>
  
  <ul>
  <li>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.</li>
  <li>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.</li>
  <li>Implement the functionality you need in a new API method. Probably not as hard as it sounds, check <a href="https://github.com/civicrm/civicrm-core/tree/master/api/v3" rel="nofollow">existing API methods for examples</a>.</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-26T10:37:14.377Z
Answers.Text: 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!
Answers.UserDisplayName: Chris Burgess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-26T10:35:59.080Z
Answers.LastActivityDate: 2016-03-26T10:35:59.080Z
Answers.UserDisplayName: Chris Burgess
Body: <p>We've created so called 'smart' groups in civicrm, and we need to know if a certrain user belongs to this group.</p>
  
  <p>I can get the groups of a user with the following code:</p>
  
  <p><a href="http://url.com/sites/all/modules/civicrm/extern/rest.php?debug=0&amp;entity=GroupContact&amp;action=get&amp;contact_id=" rel="nofollow">http://url.com/sites/all/modules/civicrm/extern/rest.php?debug=0&amp;entity=GroupContact&amp;action=get&amp;contact_id=</a>'.$uid.'&amp;json={"sequential":1}&amp;api_key='.$api_key.'&amp;key='.$site_key</p>
  
  <p>But the "Smart group of the UID is not listed here,. When I do the following:</p>
  
  <p><a href="http://url.com/sites/all/modules/civicrm/extern/rest.php?debug=0&amp;entity=Contact&amp;action=get" rel="nofollow">http://url.com/sites/all/modules/civicrm/extern/rest.php?debug=0&amp;entity=Contact&amp;action=get</a><strong>&amp;group='.$gid.'</strong>&amp;version=3&amp;contact_id='.$uid.'&amp;json={"sequential":1}&amp;api_key='.$api_key.'&amp;key='.$site_key</p>
  
  <p>I get the result, that the user belongs to this group.</p>
  
  <p>What I want is only to give the contact_id as parameter to check in which smart groups this user is</p>
  
Tags: <api><groups><smartgroups><rest>
Title: Get the "Smart" group of a user id via REST
ThreadText: # Get the "Smart" group of a user id via REST
  
  (Asked by: username_0 on 2016-03-26)
  
  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
  
  **Tags:** api, groups, smartgroups, rest
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-03-26. Score: 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](https://issues.civicrm.org/jira/browse/CRM-11903).
  
  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](https://github.com/civicrm/civicrm-core/tree/master/api/v3).
  
  > **Comments:**
  > **username_1**: 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!  
  
  ---
  
  