Resource Management API - Users

Options
Tamer Moustafa
Tamer Moustafa ✭✭✭✭
edited 04/30/24 in API & Developers

Hi Every one,
I am wondering if anyone faced this issue before.

I am trying to make an API call using the users API as following.

The issue that I don't get tags in the response as mentioned in the API documentation:

I get ''tags' in the response only when I pass it as a sting as following:

Below is a sample response after passing the entire list: ["tags", "custom_field_values"], only "custom_field_values" is there.

Answers

  • Coin Graham
    Options

    Yup, easy to work around. If you take a look at the API you'll see that they don't return custom fields with the bulk users api. You have to loop through and call with the ID for each user. Here's some code that can guide you.

    test.py

    import requests
    import os
    import time
    from datetime import datetime
    
    # Save your access token for RM in your environments variables.
    ACCESS_TOKEN = os.environ.get('TOKEN')
    BASEURL = "https://api.rm.smartsheet.com"
    USERS_API = "/api/v1/users?per_page=100&page=1&fields=email"
    CUSTOM_FIELDS_URL = "/api/v1/users/{}?fields=custom_field_values"
    
    
    def test():
        # Getting the user list
        user_api_result = requests.get("{}{}".format(BASEURL, USERS_API),
                                    headers={'Content-Type': 'application/json', 'auth': '{}'.format(ACCESS_TOKEN)})
    
        user_list = user_api_result.json()
    
        for user in user_list["data"]:
            # Sleeping here because the API has pretty restrictive limitations and I don't want to spam GChat
            time.sleep(1)
            custom_fields_url = CUSTOM_FIELDS_URL.format(user["id"])
    
            # Getting the custom fields for each user in turn
            custom_result = requests.get("{}{}".format(BASEURL, custom_fields_url),
                                         headers={'Content-Type': 'application/json',
                                                  'auth': '{}'.format(ACCESS_TOKEN)})
    
            user_custom_fields = custom_result.json()
    
            print("User: {} has the following custom fields: {}".format(user["first_name"], user_custom_fields["custom_field_values"]["data"]))
    
    
    if __name__ == '__main__':
        test()
    

    You'll note that the custom fields data is a list, so you'll need to loop through that or squash it with a list comprehension or something.

    Have fun! 😎

  • Coin Graham
    Options

    You can't pull the custom fields directly from the users get, you have to loop over the users and get their custom fields one by one. Here's some code that can help you:

    test.py

    import requests
    import os
    import time
    
    # Save your access token for RM in your environments variables.
    ACCESS_TOKEN = os.environ.get('TOKEN')
    BASEURL = "https://api.rm.smartsheet.com"
    USERS_API = "/api/v1/users?per_page=100&page=1&fields=email"
    CUSTOM_FIELDS_URL = "/api/v1/users/{}?fields=custom_field_values"
    
    
    def test():
        # Getting the user list
        user_api_result = requests.get("{}{}".format(BASEURL, USERS_API),
                                    headers={'Content-Type': 'application/json', 'auth': '{}'.format(ACCESS_TOKEN)})
    
        user_list = user_api_result.json()
    
        for user in user_list["data"]:
            # Sleeping here because the API has pretty restrictive limitations and I don't want to spam GChat
            time.sleep(1)
            custom_fields_url = CUSTOM_FIELDS_URL.format(user["id"])
    
            # Getting the custom fields for each user in turn
            custom_result = requests.get("{}{}".format(BASEURL, custom_fields_url),
                                         headers={'Content-Type': 'application/json',
                                                  'auth': '{}'.format(ACCESS_TOKEN)})
    
            user_custom_fields = custom_result.json()
    
            print("User: {} has the following custom fields: {}".format(user["first_name"], user_custom_fields["custom_field_values"]["data"]))
    
    
    if __name__ == '__main__':
        test()
    

    You'll need to loop over the custom fields and gather the data, but this should get you close enough.

    Have fun! 😎