Chilkat Online Tools

SQL Server / Cognite API v1 / Update files

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": [
    --     {
    --       "id": 4053335824259500,
    --       "update": {
    --         "externalId": {
    --           "set": "esse"
    --         },
    --         "directory": {
    --           "set": "occaecat in sit deserunt"
    --         },
    --         "source": {
    --           "set": "adipisicing in"
    --         },
    --         "mimeType": {
    --           "set": "exe"
    --         },
    --         "metadata": {
    --           "set": {}
    --         },
    --         "assetIds": {
    --           "set": [
    --           ]
    --         },
    --         "sourceCreatedTime": {
    --           "setNull": true
    --         },
    --         "sourceModifiedTime": {
    --           "setNull": true
    --         },
    --         "dataSetId": {
    --           "setNull": true
    --         },
    --         "securityCategories": {
    --           "set": [
    --           ]
    --         },
    --         "labels": {
    --           "add": [
    --             {
    --               "externalId": "sit irure reprehenderit magna labore"
    --             },
    --             {
    --               "externalId": "dolor in"
    --             }
    --           ],
    --           "remove": [
    --             {
    --               "externalId": "ut adipisicing esse laboris"
    --             },
    --             {
    --               "externalId": "sint enim sed dolor in"
    --             }
    --           ]
    --         },
    --         "geoLocation": {
    --           "set": {
    --             "type": "Feature",
    --             "geometry": {
    --               "type": "MultiPoint",
    --               "coordinates": [
    --               ]
    --             },
    --             "properties": {}
    --           }
    --         }
    --       }
    --     },
    --     {
    --       "id": 7148873468997944,
    --       "update": {
    --         "externalId": {
    --           "set": "magna consectetur irure labore"
    --         },
    --         "directory": {
    --           "setNull": true
    --         },
    --         "source": {
    --           "setNull": false
    --         },
    --         "mimeType": {
    --           "set": "pariatur exercitation laborum et do"
    --         },
    --         "metadata": {
    --           "set": {}
    --         },
    --         "assetIds": {
    --           "set": [
    --           ]
    --         },
    --         "sourceCreatedTime": {
    --           "set": -126295
    --         },
    --         "sourceModifiedTime": {
    --           "setNull": true
    --         },
    --         "dataSetId": {
    --           "setNull": false
    --         },
    --         "securityCategories": {
    --           "set": [
    --           ]
    --         },
    --         "labels": {
    --           "add": [
    --             {
    --               "externalId": "nostrud ex consequat"
    --             },
    --             {
    --               "externalId": "ullamco eiusmod elit ea"
    --             }
    --           ],
    --           "remove": [
    --             {
    --               "externalId": "deserunt dolor anim"
    --             },
    --             {
    --               "externalId": "in"
    --             }
    --           ]
    --         },
    --         "geoLocation": {
    --           "set": {
    --             "type": "Feature",
    --             "geometry": {
    --               "type": "Point",
    --               "coordinates": [
    --                 67913028.4430609,
    --                 -56262049.39717141
    --               ]
    --             },
    --             "properties": {}
    --           }
    --         }
    --       }
    --     }
    --   ]
    -- }

    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, 'UpdateInt', @success OUT, 'items[0].id', 123
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.externalId.set', 'esse'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.directory.set', 'occaecat in sit deserunt'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.source.set', 'adipisicing in'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.mimeType.set', 'exe'
    EXEC sp_OAMethod @json, 'UpdateNewObject', @success OUT, 'items[0].update.metadata.set'
    EXEC sp_OAMethod @json, 'UpdateNewArray', @success OUT, 'items[0].update.assetIds.set'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[0].update.sourceCreatedTime.setNull', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[0].update.sourceModifiedTime.setNull', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[0].update.dataSetId.setNull', 1
    EXEC sp_OAMethod @json, 'UpdateNewArray', @success OUT, 'items[0].update.securityCategories.set'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.labels.add[0].externalId', 'sit irure reprehenderit magna labore'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.labels.add[1].externalId', 'dolor in'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.labels.remove[0].externalId', 'ut adipisicing esse laboris'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.labels.remove[1].externalId', 'sint enim sed dolor in'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.geoLocation.set.type', 'Feature'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.geoLocation.set.geometry.type', 'MultiPoint'
    EXEC sp_OAMethod @json, 'UpdateNewArray', @success OUT, 'items[0].update.geoLocation.set.geometry.coordinates'
    EXEC sp_OAMethod @json, 'UpdateNewObject', @success OUT, 'items[0].update.geoLocation.set.properties'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[1].id', 123
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.externalId.set', 'magna consectetur irure labore'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[1].update.directory.setNull', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[1].update.source.setNull', 0
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.mimeType.set', 'pariatur exercitation laborum et do'
    EXEC sp_OAMethod @json, 'UpdateNewObject', @success OUT, 'items[1].update.metadata.set'
    EXEC sp_OAMethod @json, 'UpdateNewArray', @success OUT, 'items[1].update.assetIds.set'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[1].update.sourceCreatedTime.set', -126295
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[1].update.sourceModifiedTime.setNull', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[1].update.dataSetId.setNull', 0
    EXEC sp_OAMethod @json, 'UpdateNewArray', @success OUT, 'items[1].update.securityCategories.set'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.labels.add[0].externalId', 'nostrud ex consequat'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.labels.add[1].externalId', 'ullamco eiusmod elit ea'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.labels.remove[0].externalId', 'deserunt dolor anim'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.labels.remove[1].externalId', 'in'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.geoLocation.set.type', 'Feature'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.geoLocation.set.geometry.type', 'Point'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'items[1].update.geoLocation.set.geometry.coordinates[0]', '67913028.4430609'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'items[1].update.geoLocation.set.geometry.coordinates[1]', '-56262049.39717141'
    EXEC sp_OAMethod @json, 'UpdateNewObject', @success OUT, 'items[1].update.geoLocation.set.properties'

    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}}/files/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": [
        {
            "id": 4053335824259500,
            "update": {
                "externalId": {
                    "set": "esse"
                },
                "directory": {
                    "set": "occaecat in sit deserunt"
                },
                "source": {
                    "set": "adipisicing in"
                },
                "mimeType": {
                    "set": "exe"
                },
                "metadata": {
                    "set": {}
                },
                "assetIds": {
                    "set": []
                },
                "sourceCreatedTime": {
                    "setNull": true
                },
                "sourceModifiedTime": {
                    "setNull": true
                },
                "dataSetId": {
                    "setNull": true
                },
                "securityCategories": {
                    "set": []
                },
                "labels": {
                    "add": [
                        {
                            "externalId": "sit irure reprehenderit magna labore"
                        },
                        {
                            "externalId": "dolor in"
                        }
                    ],
                    "remove": [
                        {
                            "externalId": "ut adipisicing esse laboris"
                        },
                        {
                            "externalId": "sint enim sed dolor in"
                        }
                    ]
                },
                "geoLocation": {
                    "set": {
                        "type": "Feature",
                        "geometry": {
                            "type": "MultiPoint",
                            "coordinates": []
                        },
                        "properties": {}
                    }
                }
            }
        },
        {
            "id": 7148873468997944,
            "update": {
                "externalId": {
                    "set": "magna consectetur irure labore"
                },
                "directory": {
                    "setNull": true
                },
                "source": {
                    "setNull": false
                },
                "mimeType": {
                    "set": "pariatur exercitation laborum et do"
                },
                "metadata": {
                    "set": {}
                },
                "assetIds": {
                    "set": []
                },
                "sourceCreatedTime": {
                    "set": -126295
                },
                "sourceModifiedTime": {
                    "setNull": true
                },
                "dataSetId": {
                    "setNull": false
                },
                "securityCategories": {
                    "set": []
                },
                "labels": {
                    "add": [
                        {
                            "externalId": "nostrud ex consequat"
                        },
                        {
                            "externalId": "ullamco eiusmod elit ea"
                        }
                    ],
                    "remove": [
                        {
                            "externalId": "deserunt dolor anim"
                        },
                        {
                            "externalId": "in"
                        }
                    ]
                },
                "geoLocation": {
                    "set": {
                        "type": "Feature",
                        "geometry": {
                            "type": "Point",
                            "coordinates": [
                                67913028.4430609,
                                -56262049.39717141
                            ]
                        },
                        "properties": {}
                    }
                }
            }
        }
    ]
}'
https://domain.com/api/v1/projects/{{project}}/files/update

Postman Collection Item JSON

{
  "id": "updateFiles",
  "name": "Update files",
  "request": {
    "url": {
      "host": "{{baseUrl}}",
      "path": [
        "api",
        "v1",
        "projects",
        "{{project}}",
        "files",
        "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 the information for the files specified in the request body.\n\nIf you want to update the file content, uploaded using the uploadUrl, please\nuse the initFileUpload request with the query parameter 'overwrite=true'.\nAlternatively, delete and recreate the file.\n\nFor primitive fields (String, Long, Int), use 'set': 'value' to update\nvalue; use 'setNull': true to set that field to null.\n\nFor the Json Array field (e.g. assetIds and securityCategories): Use either only 'set', or a combination of 'add' and/or 'remove'. \n\n__AssetIds update examples__:\n\nExample request body to overwrite assetIds with a new set, asset ID 1 and 2.\n\n```\n{\n  \"items\": [\n    {\n      \"id\": 1,\n      \"update\": {\n        \"assetIds\" : {\n          \"set\" : [ 1, 2 ]\n        }\n      }\n    }\n  ]\n}\n```\n\nExample request body to add one asset Id, and remove another asset ID.\n\n```\n{\n  \"items\": [\n    {\n      \"id\": 1,\n      \"update\": {\n        \"assetIds\" : {\n          \"add\" : [ 3 ],\n          \"remove\": [ 2 ]\n        }\n      }\n    }\n  ]\n}\n```\n\n__Metadata update examples__:\n\nExample request body to overwrite metadata with a new set.\n```\n{\n  \"items\": [\n    {\n      \"id\": 1,\n      \"update\": {\n        \"metadata\": {\n          \"set\": {\n            \"key1\": \"value1\",\n            \"key2\": \"value2\"\n          }\n        }\n      }\n    }\n  ]\n}\n```\n\nExample request body to add two key-value pairs and remove two other key-value pairs by key for\nthe metadata field.\n```\n{\n  \"items\": [\n    {\n      \"id\": 1,\n      \"update\": {\n        \"metadata\": {\n          \"add\": {\n            \"key3\": \"value3\",\n            \"key4\": \"value4\"\n          },\n          \"remove\": [\n            \"key1\",\n            \"key2\"\n          ]\n        }\n      }\n    }\n  ]\n}\n```",
    "body": {
      "mode": "raw",
      "raw": "{\n    \"items\": [\n        {\n            \"id\": 4053335824259500,\n            \"update\": {\n                \"externalId\": {\n                    \"set\": \"esse\"\n                },\n                \"directory\": {\n                    \"set\": \"occaecat in sit deserunt\"\n                },\n                \"source\": {\n                    \"set\": \"adipisicing in\"\n                },\n                \"mimeType\": {\n                    \"set\": \"exe\"\n                },\n                \"metadata\": {\n                    \"set\": {}\n                },\n                \"assetIds\": {\n                    \"set\": []\n                },\n                \"sourceCreatedTime\": {\n                    \"setNull\": true\n                },\n                \"sourceModifiedTime\": {\n                    \"setNull\": true\n                },\n                \"dataSetId\": {\n                    \"setNull\": true\n                },\n                \"securityCategories\": {\n                    \"set\": []\n                },\n                \"labels\": {\n                    \"add\": [\n                        {\n                            \"externalId\": \"sit irure reprehenderit magna labore\"\n                        },\n                        {\n                            \"externalId\": \"dolor in\"\n                        }\n                    ],\n                    \"remove\": [\n                        {\n                            \"externalId\": \"ut adipisicing esse laboris\"\n                        },\n                        {\n                            \"externalId\": \"sint enim sed dolor in\"\n                        }\n                    ]\n                },\n                \"geoLocation\": {\n                    \"set\": {\n                        \"type\": \"Feature\",\n                        \"geometry\": {\n                            \"type\": \"MultiPoint\",\n                            \"coordinates\": []\n                        },\n                        \"properties\": {}\n                    }\n                }\n            }\n        },\n        {\n            \"id\": 7148873468997944,\n            \"update\": {\n                \"externalId\": {\n                    \"set\": \"magna consectetur irure labore\"\n                },\n                \"directory\": {\n                    \"setNull\": true\n                },\n                \"source\": {\n                    \"setNull\": false\n                },\n                \"mimeType\": {\n                    \"set\": \"pariatur exercitation laborum et do\"\n                },\n                \"metadata\": {\n                    \"set\": {}\n                },\n                \"assetIds\": {\n                    \"set\": []\n                },\n                \"sourceCreatedTime\": {\n                    \"set\": -126295\n                },\n                \"sourceModifiedTime\": {\n                    \"setNull\": true\n                },\n                \"dataSetId\": {\n                    \"setNull\": false\n                },\n                \"securityCategories\": {\n                    \"set\": []\n                },\n                \"labels\": {\n                    \"add\": [\n                        {\n                            \"externalId\": \"nostrud ex consequat\"\n                        },\n                        {\n                            \"externalId\": \"ullamco eiusmod elit ea\"\n                        }\n                    ],\n                    \"remove\": [\n                        {\n                            \"externalId\": \"deserunt dolor anim\"\n                        },\n                        {\n                            \"externalId\": \"in\"\n                        }\n                    ]\n                },\n                \"geoLocation\": {\n                    \"set\": {\n                        \"type\": \"Feature\",\n                        \"geometry\": {\n                            \"type\": \"Point\",\n                            \"coordinates\": [\n                                67913028.4430609,\n                                -56262049.39717141\n                            ]\n                        },\n                        \"properties\": {}\n                    }\n                }\n            }\n        }\n    ]\n}"
    }
  }
}