Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Save $650 on Qlik Connect, Dec 1 - 7, our lowest price of the year. Register with code CYBERWEEK: Register
cancel
Showing results for 
Search instead for 
Did you mean: 
richard_pearce6
Partner - Specialist
Partner - Specialist

Assign a Customer Group to User via Rest

Hi everyone,

I've been trying in vain to get this to work. It seems to be an invalid Patch request so maybe its something to do with my data structure. 

I have a custom group I wish to assign to a user or remove it... I'm using an API gen key against my user (who created the custom group)

code to add is

print('The group is NOT assigned to the user so add it')
        data = {"op":"add","path":"/assignedGroups","value":{"name":"Qlik Sense API"}}
        response = requests.patch(f'{tenant_url}{api_version}users/{user["id"]}', headers=headers, data=data)

 

Code to remove is:

print('The group is assigned to the user so remove it')
        data = {"op":"remove-value","path":"/assignedGroups","value":{"id":"690b6a286b0920b29e3eeb03"}}
        response = requests.patch(f'{tenant_url}{api_version}users/{user["id"]}', headers=headers, data=data)

 

I get a 400 Bad Request error: {"traceId":"3f466c9de279b0e31caab10f0524ab14","errors":[{"code":"IDENTITIES-00017","detail":"invalid request body","status":400,"title":"Bad Request"}]}

I've tried changing the data to be a string, list, string within a list, etc but no luck

Anyone solved this problem?

 

 

Labels (1)
  • API

1 Solution

Accepted Solutions
Levi_Turner
Employee
Employee

Let's create a dummy user:

POST /api/v1/users
{
  "name": "User Group Adjustment",
  "email": "user.group@corp.example",
  "subject": "1234asdasa6781231239"
}

From here, we can add a user to a _custom_ group:

PATCH /api/v1/users/692dba4bccd2c3757641e9df
[
    {
        "op": "add",
        "path": "/assignedGroups/-",
        "value": "684adfb0adbd8a0c6443bde4"
    }
]

As a side note, we can use names as well if we're using custom groups:

PATCH /api/v1/users/692dba4bccd2c3757641e9df
[
    {
        "op": "replace",
        "path": "/assignedGroups",
        "value": [
            {
                    "name": "Example2",
                    "providerType": "custom"
                }
        ]
    }
]

We can remove a _custom_ group assignment:

PATCH /api/v1/users/692dba4bccd2c3757641e9df
[
    {
        "op": "remove-value",
        "path": "/assignedGroups",
        "value": "684af6cf1c69d94a9e90ef90"
    }
]

I reference _custom_ groups very intentionally, if a user is assigned an _idp_ group then you will need to take a different approach. Rather than using the add operator to append, you will need to build the array of _existing_ groups and use the replace operator. Like so:

PATCH /api/v1/users/Rq6ftmNArJRHHyaWnFaCx5U7p8adVdUU
[
  {
    "op": "replace",
    "path": "/assignedGroups",
    "value": [
      {
        "id": "60b0fce6e585231c5e8aa755"
      },
      {
        "id": "60c7c5d74ed18c7b7f6ee173"
      },
      {
        "id": "684adfb0adbd8a0c6443bde4"
      }
    ]
  }
]

View solution in original post

4 Replies
Levi_Turner
Employee
Employee

Let's create a dummy user:

POST /api/v1/users
{
  "name": "User Group Adjustment",
  "email": "user.group@corp.example",
  "subject": "1234asdasa6781231239"
}

From here, we can add a user to a _custom_ group:

PATCH /api/v1/users/692dba4bccd2c3757641e9df
[
    {
        "op": "add",
        "path": "/assignedGroups/-",
        "value": "684adfb0adbd8a0c6443bde4"
    }
]

As a side note, we can use names as well if we're using custom groups:

PATCH /api/v1/users/692dba4bccd2c3757641e9df
[
    {
        "op": "replace",
        "path": "/assignedGroups",
        "value": [
            {
                    "name": "Example2",
                    "providerType": "custom"
                }
        ]
    }
]

We can remove a _custom_ group assignment:

PATCH /api/v1/users/692dba4bccd2c3757641e9df
[
    {
        "op": "remove-value",
        "path": "/assignedGroups",
        "value": "684af6cf1c69d94a9e90ef90"
    }
]

I reference _custom_ groups very intentionally, if a user is assigned an _idp_ group then you will need to take a different approach. Rather than using the add operator to append, you will need to build the array of _existing_ groups and use the replace operator. Like so:

PATCH /api/v1/users/Rq6ftmNArJRHHyaWnFaCx5U7p8adVdUU
[
  {
    "op": "replace",
    "path": "/assignedGroups",
    "value": [
      {
        "id": "60b0fce6e585231c5e8aa755"
      },
      {
        "id": "60c7c5d74ed18c7b7f6ee173"
      },
      {
        "id": "684adfb0adbd8a0c6443bde4"
      }
    ]
  }
]
richard_pearce6
Partner - Specialist
Partner - Specialist
Author

thanks again @Levi_Turner ! it works.. I see you're a Qlik employee.. Not sure if this is your area although on the documentation (https://qlik.dev/apis/rest/users/#patch-api-v1-users-userId)  the data seems to be written in a different format: Is this out of date? 

richard_pearce6_0-1764608789695.png

 

Levi_Turner
Employee
Employee

I do not have a say on the docs but can forward to someone who does!

richard_pearce6
Partner - Specialist
Partner - Specialist
Author

Thanks very much... I think it may help someone having a similar issue.