Assign inventory roles to device groups using the API

Hi,

Is there a way to assign an Inventory role to the newly created device group using the API?

From the documentation I was able only to find API function for creating or updating a Inventory role, but I was not able to find example of how to add this role to a specific device group.

I was able to create a device group using the API:

POST <<url>>/inventory/managedObjects
{
    "name": "TestDeviceGroup",
    "type": "c8y_DeviceGroup",
    "c8y_IsDeviceGroup": {}
}

And add the existing device to this group:

POST {{url}}/inventory/managedObjects/{{deviceGroupId}}/childAssets
{
  "managedObject": {"id":"{{deviceId}}"}
}

After executing these commands I have a device inside of newly created device group.
Inventory Role for the current user is not set for this group by default.
How could I add an Inventory role to this group for current user, using the API?

Thanks,
Nikola

After you created the new group with
POST {{url}}/inventory/managedObjects
you see in the response header the ID of the newly created group,
e.g. {{url}}/inventory/managedObjects/29102

With
POST {{url}}/user/{{tenant ID}}/users/{{user ID}}/roles/inventory
you can set an inventory role, e.g.:

{{url}}/user/t200311/users/user1/roles/inventory

Payload:
{ managedObject: "5401", roles: [ { id: 2 } ] }

“id: 2” is the ID of the inventory role.

The easiest way to find out this is to do the steps in the UI and to check in developer tools which requests are sent.

Hi Christian,

Thank you very much for your help!

Could you please elaborate more about the last chapter:
The easiest way to find out this is to do the steps in the UI and to check in developer tools which requests are sent.

Does it mean to track user action on the UI with the c8ycli tool?
What are the actions that are needed to track requests?

Best regards,
Nikola

Example: you want to find out which request is needed to list a cockpit group.
Then you go in UI and open developer tools with F12:

1 Like

In “network” tab you see the single requests and you can click them to get details.
e.g. the request URL:
https://chh.cumulocity.com/inventory/managedObjects?currentPage=1&withTotalPages=true&pageSize=20&query=%24filter%3D((type%20eq%20’c8y_DeviceGroup’))%24orderby%3Dname&withChildren=false&onlyRoots=true
and the Request Method: GET

1 Like