SQL Server Box: Create Folder

Back to Index

Creates a new folder. To create a folder in the root folder, use a parent folder id = "0".

Documentation: https://developer.box.com/reference#create-a-new-folder

CURL Command

curl https://api.box.com/2.0/folders \
-H "Authorization: Bearer BOX_ACCESS_TOKEN" \
-d '{"name":"Important Docs", "parent": {"id": "BOX_FOLDER_ID"}}' \
-X POST

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
    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

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

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'name', 'Important Docs'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'parent.id', 'BOX_FOLDER_ID'

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

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

    EXEC sp_OAMethod @json, 'EmitSb', @success OUT, STR(@sbRequestBody)
    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @rest, 'FullRequestSb', @success OUT, 'POST', '/2.0/folders', STR(@sbRequestBody), 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 @json
        EXEC @hr = sp_OADestroy @sbRequestBody
        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 @type nvarchar(4000)

    DECLARE @id nvarchar(4000)

    DECLARE @sequence_id nvarchar(4000)

    DECLARE @etag nvarchar(4000)

    DECLARE @name nvarchar(4000)

    DECLARE @created_at nvarchar(4000)

    DECLARE @modified_at nvarchar(4000)

    DECLARE @description nvarchar(4000)

    DECLARE @size int

    DECLARE @path_collectionTotal_count int

    DECLARE @created_byType nvarchar(4000)

    DECLARE @created_byId nvarchar(4000)

    DECLARE @created_byName nvarchar(4000)

    DECLARE @created_byLogin nvarchar(4000)

    DECLARE @modified_byType nvarchar(4000)

    DECLARE @modified_byId nvarchar(4000)

    DECLARE @modified_byName nvarchar(4000)

    DECLARE @modified_byLogin nvarchar(4000)

    DECLARE @trashed_at int

    DECLARE @purged_at int

    DECLARE @content_created_at nvarchar(4000)

    DECLARE @content_modified_at nvarchar(4000)

    DECLARE @owned_byType nvarchar(4000)

    DECLARE @owned_byId nvarchar(4000)

    DECLARE @owned_byName nvarchar(4000)

    DECLARE @owned_byLogin nvarchar(4000)

    DECLARE @shared_link int

    DECLARE @folder_upload_email int

    DECLARE @parentType nvarchar(4000)

    DECLARE @parentId nvarchar(4000)

    DECLARE @parentSequence_id int

    DECLARE @parentEtag int

    DECLARE @parentName nvarchar(4000)

    DECLARE @item_status nvarchar(4000)

    DECLARE @item_collectionTotal_count int

    DECLARE @item_collectionOffset int

    DECLARE @item_collectionLimit int

    DECLARE @i int

    DECLARE @count_i int

    DECLARE @sequence_id_bool int

    DECLARE @etag_bool int

    DECLARE @by nvarchar(4000)

    DECLARE @direction nvarchar(4000)

    EXEC sp_OAMethod @jsonResponse, 'StringOf', @type OUT, 'type'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @id OUT, 'id'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @sequence_id OUT, 'sequence_id'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @etag OUT, 'etag'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @name OUT, 'name'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @created_at OUT, 'created_at'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @modified_at OUT, 'modified_at'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @description OUT, 'description'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @size OUT, 'size'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @path_collectionTotal_count OUT, 'path_collection.total_count'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @created_byType OUT, 'created_by.type'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @created_byId OUT, 'created_by.id'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @created_byName OUT, 'created_by.name'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @created_byLogin OUT, 'created_by.login'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @modified_byType OUT, 'modified_by.type'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @modified_byId OUT, 'modified_by.id'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @modified_byName OUT, 'modified_by.name'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @modified_byLogin OUT, 'modified_by.login'
    EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @trashed_at OUT, 'trashed_at'
    EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @purged_at OUT, 'purged_at'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @content_created_at OUT, 'content_created_at'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @content_modified_at OUT, 'content_modified_at'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @owned_byType OUT, 'owned_by.type'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @owned_byId OUT, 'owned_by.id'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @owned_byName OUT, 'owned_by.name'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @owned_byLogin OUT, 'owned_by.login'
    EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @shared_link OUT, 'shared_link'
    EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @folder_upload_email OUT, 'folder_upload_email'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @parentType OUT, 'parent.type'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @parentId OUT, 'parent.id'
    EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @parentSequence_id OUT, 'parent.sequence_id'
    EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @parentEtag OUT, 'parent.etag'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @parentName OUT, 'parent.name'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @item_status OUT, 'item_status'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @item_collectionTotal_count OUT, 'item_collection.total_count'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @item_collectionOffset OUT, 'item_collection.offset'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @item_collectionLimit OUT, 'item_collection.limit'
    SELECT @i = 0
    EXEC sp_OAMethod @jsonResponse, 'SizeOfArray', @count_i OUT, 'path_collection.entries'
    WHILE STR(@i) < STR(@count_i)
      BEGIN
        EXEC sp_OASetProperty @jsonResponse, 'I', STR(@i)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @type OUT, 'path_collection.entries[i].type'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @id OUT, 'path_collection.entries[i].id'
        EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @sequence_id_bool OUT, 'path_collection.entries[i].sequence_id'
        EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @etag_bool OUT, 'path_collection.entries[i].etag'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @name OUT, 'path_collection.entries[i].name'
        SELECT @i = STR(@i) + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @jsonResponse, 'SizeOfArray', @count_i OUT, 'item_collection.entries'
    WHILE STR(@i) < STR(@count_i)
      BEGIN
        EXEC sp_OASetProperty @jsonResponse, 'I', STR(@i)
        SELECT @i = STR(@i) + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @jsonResponse, 'SizeOfArray', @count_i OUT, 'item_collection.order'
    WHILE STR(@i) < STR(@count_i)
      BEGIN
        EXEC sp_OASetProperty @jsonResponse, 'I', STR(@i)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @by OUT, 'item_collection.order[i].by'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @direction OUT, 'item_collection.order[i].direction'
        SELECT @i = STR(@i) + 1
      END

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


END
GO

Sample JSON Response Body

{
  "type": "folder",
  "id": "47885473705",
  "sequence_id": "0",
  "etag": "0",
  "name": "Important Docs",
  "created_at": "2018-03-16T06:54:57-07:00",
  "modified_at": "2018-03-16T06:54:57-07:00",
  "description": "",
  "size": 0,
  "path_collection": {
    "total_count": 1,
    "entries": [
      {
        "type": "folder",
        "id": "0",
        "sequence_id": null,
        "etag": null,
        "name": "All Files"
      }
    ]
  },
  "created_by": {
    "type": "user",
    "id": "2787704945",
    "name": "chilkat",
    "login": "AutomationUser_434741_3nmGYSS7o5@@boxdevedition.com"
  },
  "modified_by": {
    "type": "user",
    "id": "2787704945",
    "name": "chilkat",
    "login": "AutomationUser_434741_3nmGYSS7o5@@boxdevedition.com"
  },
  "trashed_at": null,
  "purged_at": null,
  "content_created_at": "2018-03-16T06:54:57-07:00",
  "content_modified_at": "2018-03-16T06:54:57-07:00",
  "owned_by": {
    "type": "user",
    "id": "2787704945",
    "name": "chilkat",
    "login": "AutomationUser_434741_3nmGYSS7o5@@boxdevedition.com"
  },
  "shared_link": null,
  "folder_upload_email": null,
  "parent": {
    "type": "folder",
    "id": "0",
    "sequence_id": null,
    "etag": null,
    "name": "All Files"
  },
  "item_status": "active",
  "item_collection": {
    "total_count": 0,
    "entries": [
    ],
    "offset": 0,
    "limit": 100,
    "order": [
      {
        "by": "type",
        "direction": "ASC"
      },
      {
        "by": "name",
        "direction": "ASC"
      }
    ]
  }
}