Forum Discussion

keithkelly's avatar
2 years ago

Impossible to list User Roles from single "from users" API call?

I'm trying to emulate the User Report that lists users, ids, etc & roles like so:

SELECT login, id, topics.count(*), messages.count(*) FROM users

including "roles" just gives the implicit join query.

Is there any way to also grab the individual roles of the users from 1 API v2 query?  

  • Yeah, the role is not in users but in roles:

    SELECT name FROM roles WHERE users.id = ''

    I have made a component that displays the role as well the rank in the avatar section of a message. For non-admin users to see the role, it was needed to perform above query using restadmin instead rest.

    • keithkelly's avatar
      keithkelly
      Leader

      Holy **bleep** it worked!  

      Here's a simple working Python script:

      import json
      import requests
      
      headers = {
          "li-api-session-key": "XXX",
          "Content-Type": "application/json"
      }
      
      data = {
          "users": {
              "fields": ["login","roles"],
              "constraints": [],
              "subQueries": {
                  "roles": {
                      "fields": ["id", "name"],
                      "limit": 20
                  }
              }
          }
      }
      
      response = requests.post("https://[your community]/api/2.0/search", headers=headers, json=data)
      
      print(response.json())

       

      I'm looking forward to tweaking it a bit and using this in Excel or PowerBI PowerQuery.  Thanks!