Chilkat Online Tools

SQL Server / Cognite API v1 / Update assets

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    -- Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    -- The following JSON is sent in the request body.

    -- {
    --   "items": [
    --     {
    --       "update": {
    --         "externalId": {
    --           "set": "ad"
    --         },
    --         "name": {
    --           "set": "incididunt proident in"
    --         },
    --         "description": {
    --           "setNull": true
    --         },
    --         "dataSetId": {
    --           "set": -49836609
    --         },
    --         "metadata": {
    --           "set": {}
    --         },
    --         "source": {
    --           "set": "sed laboris ut"
    --         },
    --         "parentId": {
    --           "set": 1285179828449184
    --         },
    --         "parentExternalId": {
    --           "set": "cupidatat"
    --         },
    --         "labels": {
    --           "set": [
    --             {
    --               "externalId": "sit anim ipsum"
    --             },
    --             {
    --               "externalId": "fugiat cillum"
    --             }
    --           ]
    --         }
    --       },
    --       "externalId": "deserunt "
    --     },
    --     {
    --       "update": {
    --         "externalId": {
    --           "setNull": false
    --         },
    --         "name": {
    --           "set": "aute"
    --         },
    --         "description": {
    --           "setNull": true
    --         },
    --         "dataSetId": {
    --           "set": -68427017
    --         },
    --         "metadata": {
    --           "add": {},
    --           "remove": [
    --           ]
    --         },
    --         "source": {
    --           "set": "incididunt proident aute consequat"
    --         },
    --         "parentId": {
    --           "set": 4455735300348148
    --         },
    --         "parentExternalId": {
    --           "set": "quis est mollit esse"
    --         },
    --         "labels": {
    --           "add": [
    --             {
    --               "externalId": "officia pariatur mollit sunt"
    --             },
    --             {
    --               "externalId": "pariatur veniam"
    --             }
    --           ],
    --           "remove": [
    --             {
    --               "externalId": "dolor nulla dolor non"
    --             },
    --             {
    --               "externalId": "ex"
    --             }
    --           ]
    --         }
    --       },
    --       "id": 7396120406053176
    --     }
    --   ]
    -- }

    DECLARE @json int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.externalId.set', 'ad'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.name.set', 'incididunt proident in'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[0].update.description.setNull', 1
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[0].update.dataSetId.set', -49836609
    EXEC sp_OAMethod @json, 'UpdateNewObject', @success OUT, 'items[0].update.metadata.set'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.source.set', 'sed laboris ut'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[0].update.parentId.set', 123
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.parentExternalId.set', 'cupidatat'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.labels.set[0].externalId', 'sit anim ipsum'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.labels.set[1].externalId', 'fugiat cillum'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].externalId', 'deserunt '
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[1].update.externalId.setNull', 0
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.name.set', 'aute'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[1].update.description.setNull', 1
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[1].update.dataSetId.set', -68427017
    EXEC sp_OAMethod @json, 'UpdateNewObject', @success OUT, 'items[1].update.metadata.add'
    EXEC sp_OAMethod @json, 'UpdateNewArray', @success OUT, 'items[1].update.metadata.remove'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.source.set', 'incididunt proident aute consequat'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[1].update.parentId.set', 123
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.parentExternalId.set', 'quis est mollit esse'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.labels.add[0].externalId', 'officia pariatur mollit sunt'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.labels.add[1].externalId', 'pariatur veniam'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.labels.remove[0].externalId', 'dolor nulla dolor non'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.labels.remove[1].externalId', 'ex'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[1].id', 123

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'content-type', 'application/json'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'api-key', '{{api-key}}'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/api/v1/projects/{{project}}/assets/update', 'application/json', @json
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json


END
GO

Curl Command

curl -X POST
	-H "api-key: {{api-key}}"
	-H "content-type: application/json"
	-d '{
    "items": [
        {
            "update": {
                "externalId": {
                    "set": "ad"
                },
                "name": {
                    "set": "incididunt proident in"
                },
                "description": {
                    "setNull": true
                },
                "dataSetId": {
                    "set": -49836609
                },
                "metadata": {
                    "set": {}
                },
                "source": {
                    "set": "sed laboris ut"
                },
                "parentId": {
                    "set": 1285179828449184
                },
                "parentExternalId": {
                    "set": "cupidatat"
                },
                "labels": {
                    "set": [
                        {
                            "externalId": "sit anim ipsum"
                        },
                        {
                            "externalId": "fugiat cillum"
                        }
                    ]
                }
            },
            "externalId": "deserunt "
        },
        {
            "update": {
                "externalId": {
                    "setNull": false
                },
                "name": {
                    "set": "aute"
                },
                "description": {
                    "setNull": true
                },
                "dataSetId": {
                    "set": -68427017
                },
                "metadata": {
                    "add": {},
                    "remove": []
                },
                "source": {
                    "set": "incididunt proident aute consequat"
                },
                "parentId": {
                    "set": 4455735300348148
                },
                "parentExternalId": {
                    "set": "quis est mollit esse"
                },
                "labels": {
                    "add": [
                        {
                            "externalId": "officia pariatur mollit sunt"
                        },
                        {
                            "externalId": "pariatur veniam"
                        }
                    ],
                    "remove": [
                        {
                            "externalId": "dolor nulla dolor non"
                        },
                        {
                            "externalId": "ex"
                        }
                    ]
                }
            },
            "id": 7396120406053176
        }
    ]
}'
https://domain.com/api/v1/projects/{{project}}/assets/update

Postman Collection Item JSON

{
  "id": "updateAssets",
  "name": "Update assets",
  "request": {
    "url": {
      "host": "{{baseUrl}}",
      "path": [
        "api",
        "v1",
        "projects",
        "{{project}}",
        "assets",
        "update"
      ],
      "query": [
      ],
      "variable": [
      ]
    },
    "method": "POST",
    "header": [
      {
        "key": "api-key",
        "value": "{{api-key}}",
        "description": "An admin can create API keys in the Cognite console."
      },
      {
        "key": "content-type",
        "value": "application/json"
      }
    ],
    "description": "Update the attributes of assets.",
    "body": {
      "mode": "raw",
      "raw": "{\n    \"items\": [\n        {\n            \"update\": {\n                \"externalId\": {\n                    \"set\": \"ad\"\n                },\n                \"name\": {\n                    \"set\": \"incididunt proident in\"\n                },\n                \"description\": {\n                    \"setNull\": true\n                },\n                \"dataSetId\": {\n                    \"set\": -49836609\n                },\n                \"metadata\": {\n                    \"set\": {}\n                },\n                \"source\": {\n                    \"set\": \"sed laboris ut\"\n                },\n                \"parentId\": {\n                    \"set\": 1285179828449184\n                },\n                \"parentExternalId\": {\n                    \"set\": \"cupidatat\"\n                },\n                \"labels\": {\n                    \"set\": [\n                        {\n                            \"externalId\": \"sit anim ipsum\"\n                        },\n                        {\n                            \"externalId\": \"fugiat cillum\"\n                        }\n                    ]\n                }\n            },\n            \"externalId\": \"deserunt \"\n        },\n        {\n            \"update\": {\n                \"externalId\": {\n                    \"setNull\": false\n                },\n                \"name\": {\n                    \"set\": \"aute\"\n                },\n                \"description\": {\n                    \"setNull\": true\n                },\n                \"dataSetId\": {\n                    \"set\": -68427017\n                },\n                \"metadata\": {\n                    \"add\": {},\n                    \"remove\": []\n                },\n                \"source\": {\n                    \"set\": \"incididunt proident aute consequat\"\n                },\n                \"parentId\": {\n                    \"set\": 4455735300348148\n                },\n                \"parentExternalId\": {\n                    \"set\": \"quis est mollit esse\"\n                },\n                \"labels\": {\n                    \"add\": [\n                        {\n                            \"externalId\": \"officia pariatur mollit sunt\"\n                        },\n                        {\n                            \"externalId\": \"pariatur veniam\"\n                        }\n                    ],\n                    \"remove\": [\n                        {\n                            \"externalId\": \"dolor nulla dolor non\"\n                        },\n                        {\n                            \"externalId\": \"ex\"\n                        }\n                    ]\n                }\n            },\n            \"id\": 7396120406053176\n        }\n    ]\n}"
    }
  }
}