Chilkat Online Tools

SQL Server / Cognite API v1 / Update events

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": {
    --           "setNull": false
    --         },
    --         "dataSetId": {
    --           "setNull": false
    --         },
    --         "startTime": {
    --           "setNull": true
    --         },
    --         "endTime": {
    --           "setNull": false
    --         },
    --         "description": {
    --           "set": "dolor cillum velit ullamco id"
    --         },
    --         "metadata": {
    --           "set": {}
    --         },
    --         "assetIds": {
    --           "add": [
    --           ],
    --           "remove": [
    --           ]
    --         },
    --         "source": {
    --           "setNull": true
    --         },
    --         "type": {
    --           "setNull": true
    --         },
    --         "subtype": {
    --           "set": "Ut Duis dolor esse"
    --         }
    --       },
    --       "id": 4891475566776272
    --     },
    --     {
    --       "update": {
    --         "externalId": {
    --           "set": "aliquip sit aute"
    --         },
    --         "dataSetId": {
    --           "set": -95510276
    --         },
    --         "startTime": {
    --           "setNull": false
    --         },
    --         "endTime": {
    --           "setNull": false
    --         },
    --         "description": {
    --           "set": "in dolore aliquip dolor proident"
    --         },
    --         "metadata": {
    --           "set": {}
    --         },
    --         "assetIds": {
    --           "add": [
    --           ],
    --           "remove": [
    --           ]
    --         },
    --         "source": {
    --           "set": "dolore nostrud"
    --         },
    --         "type": {
    --           "set": "ea"
    --         },
    --         "subtype": {
    --           "set": "aute"
    --         }
    --       },
    --       "id": 7760910451842418
    --     }
    --   ]
    -- }

    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, 'UpdateBool', @success OUT, 'items[0].update.externalId.setNull', 0
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[0].update.dataSetId.setNull', 0
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[0].update.startTime.setNull', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[0].update.endTime.setNull', 0
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.description.set', 'dolor cillum velit ullamco id'
    EXEC sp_OAMethod @json, 'UpdateNewObject', @success OUT, 'items[0].update.metadata.set'
    EXEC sp_OAMethod @json, 'UpdateNewArray', @success OUT, 'items[0].update.assetIds.add'
    EXEC sp_OAMethod @json, 'UpdateNewArray', @success OUT, 'items[0].update.assetIds.remove'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[0].update.source.setNull', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[0].update.type.setNull', 1
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.subtype.set', 'Ut Duis dolor esse'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[0].id', 123
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.externalId.set', 'aliquip sit aute'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[1].update.dataSetId.set', -95510276
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[1].update.startTime.setNull', 0
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[1].update.endTime.setNull', 0
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.description.set', 'in dolore aliquip dolor proident'
    EXEC sp_OAMethod @json, 'UpdateNewObject', @success OUT, 'items[1].update.metadata.set'
    EXEC sp_OAMethod @json, 'UpdateNewArray', @success OUT, 'items[1].update.assetIds.add'
    EXEC sp_OAMethod @json, 'UpdateNewArray', @success OUT, 'items[1].update.assetIds.remove'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.source.set', 'dolore nostrud'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.type.set', 'ea'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.subtype.set', 'aute'
    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}}/events/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": {
                    "setNull": false
                },
                "dataSetId": {
                    "setNull": false
                },
                "startTime": {
                    "setNull": true
                },
                "endTime": {
                    "setNull": false
                },
                "description": {
                    "set": "dolor cillum velit ullamco id"
                },
                "metadata": {
                    "set": {}
                },
                "assetIds": {
                    "add": [],
                    "remove": []
                },
                "source": {
                    "setNull": true
                },
                "type": {
                    "setNull": true
                },
                "subtype": {
                    "set": "Ut Duis dolor esse"
                }
            },
            "id": 4891475566776272
        },
        {
            "update": {
                "externalId": {
                    "set": "aliquip sit aute"
                },
                "dataSetId": {
                    "set": -95510276
                },
                "startTime": {
                    "setNull": false
                },
                "endTime": {
                    "setNull": false
                },
                "description": {
                    "set": "in dolore aliquip dolor proident"
                },
                "metadata": {
                    "set": {}
                },
                "assetIds": {
                    "add": [],
                    "remove": []
                },
                "source": {
                    "set": "dolore nostrud"
                },
                "type": {
                    "set": "ea"
                },
                "subtype": {
                    "set": "aute"
                }
            },
            "id": 7760910451842418
        }
    ]
}'
https://domain.com/api/v1/projects/{{project}}/events/update

Postman Collection Item JSON

{
  "id": "updateEvents",
  "name": "Update events",
  "request": {
    "url": {
      "host": "{{baseUrl}}",
      "path": [
        "api",
        "v1",
        "projects",
        "{{project}}",
        "events",
        "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": "Updates events in the same project. This operation supports partial updates; Fields omitted from queries will remain unchanged on objects.\n\nFor primitive fields (String, Long, Int), use 'set': 'value' to update value; use 'setNull': true to set that field to null.\n\nFor the Json Array field (e.g. assetIds), use 'set': [value1, value2] to update value; use 'add': [v1, v2] to add values to current list of values; use 'remove': [v1, v2] to remove these values from current list of values if exists.\n\nA maximum of 1000 events can be updated per request, and all of the event IDs must be unique.",
    "body": {
      "mode": "raw",
      "raw": "{\n    \"items\": [\n        {\n            \"update\": {\n                \"externalId\": {\n                    \"setNull\": false\n                },\n                \"dataSetId\": {\n                    \"setNull\": false\n                },\n                \"startTime\": {\n                    \"setNull\": true\n                },\n                \"endTime\": {\n                    \"setNull\": false\n                },\n                \"description\": {\n                    \"set\": \"dolor cillum velit ullamco id\"\n                },\n                \"metadata\": {\n                    \"set\": {}\n                },\n                \"assetIds\": {\n                    \"add\": [],\n                    \"remove\": []\n                },\n                \"source\": {\n                    \"setNull\": true\n                },\n                \"type\": {\n                    \"setNull\": true\n                },\n                \"subtype\": {\n                    \"set\": \"Ut Duis dolor esse\"\n                }\n            },\n            \"id\": 4891475566776272\n        },\n        {\n            \"update\": {\n                \"externalId\": {\n                    \"set\": \"aliquip sit aute\"\n                },\n                \"dataSetId\": {\n                    \"set\": -95510276\n                },\n                \"startTime\": {\n                    \"setNull\": false\n                },\n                \"endTime\": {\n                    \"setNull\": false\n                },\n                \"description\": {\n                    \"set\": \"in dolore aliquip dolor proident\"\n                },\n                \"metadata\": {\n                    \"set\": {}\n                },\n                \"assetIds\": {\n                    \"add\": [],\n                    \"remove\": []\n                },\n                \"source\": {\n                    \"set\": \"dolore nostrud\"\n                },\n                \"type\": {\n                    \"set\": \"ea\"\n                },\n                \"subtype\": {\n                    \"set\": \"aute\"\n                }\n            },\n            \"id\": 7760910451842418\n        }\n    ]\n}"
    }
  }
}