Delete widget programmatically

Hi,
it’s Luca from DAC System.
This question is related to this former topic.

For automatic testing porpouses, we need to modify a dashboard programmatically. In particular, at the moment we need to delete a map widget.

The tests are perfomed on the home section of a custom Cockpit application. So, the application looks like this

On the inventory collection the home looks like this (im using Postman to simulate what I would code):

  {
            "additionParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/additionParents"
            },
            "owner": "kai.reinhardt@softwareag.com",
            "childDevices": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/childDevices"
            },
            "childAssets": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/childAssets"
            },
            "creationTime": "2022-02-22T08:10:20.786Z",
            "lastUpdated": "2023-12-18T09:10:57.719Z",
            "childAdditions": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/childAdditions"
            },
            "assetParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/assetParents"
            },
            "deviceParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/deviceParents"
            },
            "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200",
            "id": "1200",
            "c8y_Dashboard!name!home-cockpit1": {},
            "c8y_Global": {},
            "c8y_Dashboard": {
                "children": {
                    "799324260951046": {
                        "componentId": "cluster-map-widget",
                        "classes": {
                            "card-dashboard": true,
                            "panel-title-regular": true,
                            "cluster-map-widget": true,
                            "card": true
                        },
                        "_x": 0,
                        "_y": 0,
                        "id": "799324260951046",
                        "title": "Cluster Map",
                        "_width": 7,
                        "config": {},
                        "_height": 7
                    }
                },
                "classes": {
                    "dashboard-theme-transparent": true
                },
                "c8y_IsNavigatorNode": null,
                "widgetClasses": {
                    "panel-title-regular": true
                },
                "isFrozen": false
            }
 }

My initial approach is to get the id of the cluster-map-widget and just delete it. To know if I’m getting things right I try to retrieve the widget with the following query

/inventory/managedObjects/7967897646768485

Result:

{
    "error": "inventory/Not Found",
    "message": "Finding device data from database failed : No managedObject for id '7967897646768485'!",
    "info": "https://www.cumulocity.com/guides/reference/rest-implementation//#a-name-error-reporting-a-error-reporting"
}

So widgets instances are not MO (?). If so I cannot use the inventory API to delete a widget even though I know its Id.

The only solution I see to this problem is to get the home MO and update it. Any better option? Am I missing something again?

BONUS: to retrieve the Home MO I’m performing this query:

/inventory/managedObjects?fragmentType=c8y_Dashboard!name!home-cockpit1

Any better solution? Is this ok?

Best
Luca

Hi Luca,

I do not really get where you’ve taken the 7967897646768485 id from…

The widget instances are no managedObjects. They are stored in the c8y_Dashboard.children path of the dashboard managedObject.
In case you want to remove/edit a widget instance of a dashboard, you would have to adjust the dashboard managedObject.

Instead of updating the managedObject, you could also delete the dashboard and create a new one with the state you actually want to verify.
If you do not create a new dashboard managedObject, the UI will create a new one (containing the default widgets) once you visit the Home dashboard the next time.

The query that is performed in the UI is: /inventory/managedObjects?fragmentType=c8y_Dashboard!name!home-cockpit1&pageSize=1. You could use the same or stick with yours, the pageSize shouldn’t make a lot of difference.

Regards,
Tristan

I do not really get where you’ve taken the 7967897646768485 id from…

My bad, copy paste error. In any case, that would be the widget id in the home MO

image

Ok that’s fine. Atm I’m performing an update as I said and it works. Eventually this could be the wrong approach when dashboard structure will become more complex. I’ll try to delete and create as you suggest…

Thank you!
Best
Luca

Again, about the query to get the Home Managed Object:

/inventory/managedObjects?fragmentType=c8y_Dashboard!name!home-cockpit1

The result is:

{
    "next": "https://t1358926305.cumulocity.com/inventory/managedObjects?withTotalElements=true&pageSize=50&fragmentType=c8y_Dashboard!name!home-cockpit1&currentPage=2",
    "self": "https://t1358926305.cumulocity.com/inventory/managedObjects?withTotalElements=true&pageSize=50&fragmentType=c8y_Dashboard!name!home-cockpit1&currentPage=1",
    "managedObjects": [
        {
            "additionParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/additionParents"
            },
            "owner": "kai.reinhardt@softwareag.com",
            "childDevices": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/childDevices"
            },
            "childAssets": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/childAssets"
            },
            "creationTime": "2022-02-22T08:10:20.786Z",
            "lastUpdated": "2023-12-20T10:04:54.974Z",
            "childAdditions": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/childAdditions"
            },
            "deviceParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/deviceParents"
            },
            "assetParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/assetParents"
            },
            "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200",
            "id": "1200",
            "c8y_Dashboard!name!home-cockpit1": {},
            "c8y_Global": {},
            "c8y_Dashboard": {
                "children": {},
                "classes": {
                    "dashboard-theme-transparent": true
                },
                "c8y_IsNavigatorNode": null,
                "widgetClasses": {
                    "panel-title-regular": true
                },
                "isFrozen": false
            }
        },
        {
            "additionParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1016029/additionParents"
            },
            "owner": "kai.reinhardt@softwareag.com",
            "childDevices": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1016029/childDevices"
            },
            "childAssets": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1016029/childAssets"
            },
            "creationTime": "2022-03-11T09:23:06.703Z",
            "lastUpdated": "2023-12-20T10:04:54.979Z",
            "childAdditions": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1016029/childAdditions"
            },
            "deviceParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1016029/deviceParents"
            },
            "assetParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1016029/assetParents"
            },
            "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1016029",
            "id": "1016029",
            "c8y_Dashboard!name!home-cockpit1": {},
            "c8y_Global": {},
            "c8y_Dashboard": {
                "children": {},
                "widgetClasses": {
                    "dashboard-theme-light": true,
                    "panel-title-regular": true
                }
            }
        },
        {
            "additionParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/3565046/additionParents"
            },
            "owner": "luca.brena@dacsystem.ch",
            "childDevices": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/3565046/childDevices"
            },
            "childAssets": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/3565046/childAssets"
            },
            "creationTime": "2022-03-29T10:30:07.839Z",
            "lastUpdated": "2023-12-20T10:04:54.980Z",
            "childAdditions": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/3565046/childAdditions"
            },
            "deviceParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/3565046/deviceParents"
            },
            "assetParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/3565046/assetParents"
            },
            "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/3565046",
            "id": "3565046",
            "c8y_Dashboard!name!home-cockpit1": {},
            "c8y_Global": {},
            "c8y_Dashboard": {
                "children": {},
                "widgetClasses": {
                    "dashboard-theme-light": true,
                    "panel-title-regular": true
                }
            }
        },
        {
            "additionParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/34106816/additionParents"
            },
            "owner": "maurizio.santovito@dacsystem.ch",
            "childDevices": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/34106816/childDevices"
            },
            "childAssets": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/34106816/childAssets"
            },
            "creationTime": "2022-08-09T17:02:14.532Z",
            "lastUpdated": "2023-12-20T10:04:54.977Z",
            "childAdditions": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/34106816/childAdditions"
            },
            "deviceParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/34106816/deviceParents"
            },
            "assetParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/34106816/assetParents"
            },
            "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/34106816",
            "id": "34106816",
            "c8y_Dashboard!name!home-cockpit1": {},
            "c8y_Global": {},
            "c8y_Dashboard": {
                "children": {},
                "widgetClasses": {
                    "dashboard-theme-light": true,
                    "panel-title-regular": true
                }
            }
        }
    ],
    "statistics": {
        "pageSize": 50,
        "currentPage": 1,
        "totalElements": 4
    }
}

As you can see we have 4 different homes. How can I target the home from a specific application?

Taking a look at what the browser calls

and I spot some interesting headers, which seem to refer to the application under test called Dashboard

No luck trying to reproduce this call

Any suggestion?

Best

The Referer and X-Cumulocity-Application-Key do not influence the response you are receiving here.

I can’t really tell why you are having multiple dashboards with the c8y_Dashboard!name!home-cockpit1 fragment, there should actually just be a single one.
This might be related to the limited permissions you are giving to the other user accounts, but I’m not sure.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.