SQL Server Box: Get Folder Items

Back to Index

Gets all of the files, folders, or web links contained within a folder.

Documentation: https://developer.box.com/reference#get-a-folders-items

CURL Command

curl https://api.box.com/2.0/folders/BOX_FOLDER_ID/items?limit=2&offset=0 \
-H "Authorization: Bearer BOX_ACCESS_TOKEN"

SQL Server Example

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Rest', @rest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    --  URL: https://api.box.com/2.0/folders/BOX_FOLDER_ID/items?limit=2&offset=0
    DECLARE @bTls int
    SELECT @bTls = 1
    DECLARE @port int
    SELECT @port = 443
    DECLARE @bAutoReconnect int
    SELECT @bAutoReconnect = 1
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'api.box.com', STR(@port), STR(@bTls), STR(@bAutoReconnect)
    IF STR(@success) <> 1
      BEGIN

        EXEC sp_OAGetProperty @rest, 'ConnectFailReason', @iTmp0 OUT
        PRINT 'ConnectFailReason: ' + @iTmp0
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Authorization', 'Bearer BOX_ACCESS_TOKEN'

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @rest, 'FullRequestNoBodySb', @success OUT, 'GET', '/2.0/folders/BOX_FOLDER_ID/items?limit=2&offset=0', STR(@sbResponseBody)
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END

    DECLARE @jsonResponse int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jsonResponse OUT

    EXEC sp_OAMethod @jsonResponse, 'LoadSb', @success OUT, STR(@sbResponseBody)

    DECLARE @total_count int

    DECLARE @offset int

    DECLARE @limit int

    DECLARE @i int

    DECLARE @count_i int

    DECLARE @type nvarchar(4000)

    DECLARE @id nvarchar(4000)

    DECLARE @file_versionType nvarchar(4000)

    DECLARE @file_versionId nvarchar(4000)

    DECLARE @file_versionSha1 nvarchar(4000)

    DECLARE @sequence_id nvarchar(4000)

    DECLARE @etag nvarchar(4000)

    DECLARE @sha1 nvarchar(4000)

    DECLARE @name nvarchar(4000)

    DECLARE @by nvarchar(4000)

    DECLARE @direction nvarchar(4000)

    EXEC sp_OAMethod @jsonResponse, 'IntOf', @total_count OUT, 'total_count'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @offset OUT, 'offset'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @limit OUT, 'limit'
    SELECT @i = 0
    EXEC sp_OAMethod @jsonResponse, 'SizeOfArray', @count_i OUT, 'entries'
    WHILE STR(@i) < STR(@count_i)
      BEGIN
        EXEC sp_OASetProperty @jsonResponse, 'I', STR(@i)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @type OUT, 'entries[i].type'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @id OUT, 'entries[i].id'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @file_versionType OUT, 'entries[i].file_version.type'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @file_versionId OUT, 'entries[i].file_version.id'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @file_versionSha1 OUT, 'entries[i].file_version.sha1'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @sequence_id OUT, 'entries[i].sequence_id'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @etag OUT, 'entries[i].etag'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @sha1 OUT, 'entries[i].sha1'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @name OUT, 'entries[i].name'
        SELECT @i = STR(@i) + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @jsonResponse, 'SizeOfArray', @count_i OUT, 'order'
    WHILE STR(@i) < STR(@count_i)
      BEGIN
        EXEC sp_OASetProperty @jsonResponse, 'I', STR(@i)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @by OUT, 'order[i].by'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @direction OUT, 'order[i].direction'
        SELECT @i = STR(@i) + 1
      END

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jsonResponse


END
GO

Sample JSON Response Body

{
  "total_count": 2,
  "entries": [
    {
      "type": "file",
      "id": "246181882790",
      "file_version": {
        "type": "file_version",
        "id": "259636211878",
        "sha1": "c9d2492fb97f88a9b4d1e35f32a3410e95853f18"
      },
      "sequence_id": "0",
      "etag": "0",
      "sha1": "c9d2492fb97f88a9b4d1e35f32a3410e95853f18",
      "name": "hedgehogs.jpg"
    },
    {
      "type": "file",
      "id": "246167973161",
      "file_version": {
        "type": "file_version",
        "id": "259621592361",
        "sha1": "df7be9dc4f467187783aca68c7ce98e4df2172d0"
      },
      "sequence_id": "0",
      "etag": "0",
      "sha1": "df7be9dc4f467187783aca68c7ce98e4df2172d0",
      "name": "penguins.jpg"
    }
  ],
  "offset": 0,
  "limit": 2,
  "order": [
    {
      "by": "type",
      "direction": "ASC"
    },
    {
      "by": "name",
      "direction": "ASC"
    }
  ]
}