Transfer Files & Folders

Transfer Files & Folders

As part of user account deprovisioning, a common requirement is to transfer all files and folders that are stored within the user account to another user account or into a location for long term storage, such as into the service account.

There are two general methods that are employed to accomplish this within Box:

  • Using the direct transfer owned folders API, which will move all content from one user directly to another.
  • Using the collaboration transfer method to change ownership of one file or folder at a time from one user to another.

Transfer Owned Folders API Method

The transfer owned folders endpoint is designed to move the entirety of content owned by one user over to another user.

The transfer owned folders API is performed as a synchronous process, which might lead to a slow response when the source user has a large number of items in all of its folders.

To call the transfer endpoint, you will supply the user ID to transfer from and the user ID to transfer to.

cURL
curl -X PUT https://api.box.com/2.0/users/12345/folders/0 \
     -H 'Authorization: Bearer <ACCESS_TOKEN>'
     -H 'Content-Type: application/json" '
     -d '{
       "owned_by": {
         "id": "1232234"
       }
     }'
.NET
var sourceUserId = "33333";
var destinationUserId = "44444";
BoxFolder movedFolder = await client.MoveUserFolderAsync(sourceUserId, destinationUserId);
Java
String sourceUserID = "11111";
String destinationUserID = "22222";
BoxUser sourceUser = new BoxUser(api, sourceUserID);
BoxFolder.Info transferredFolderInfo = sourceUser.transferContent(destinationUserID);
Python
source_user_id = '33333'
destination_user_id = '44444'

user = client.user(source_user_id)
destination_user = client.user(destination_user_id)

folder = user.transfer_content(destination_user)
print('Created new folder "{0}" in the account of user {1}'.format(folder.name, destination_user.id))
Node
var sourceUserID = '33333';
var destinationUserID = '44444';
client.enterprise.transferUserContent(sourceUserID, destinationUserID)
	.then(movedFolder => {
		/* movedFolder -> {
			type: 'folder',
			id: '123456789',
			sequence_id: '1',
			etag: '1',
			name: 'Other User's Files and Folders',
			created_at: '2018-04-23T11:00:07-07:00',
			modified_at: '2018-04-23T11:00:07-07:00',
			description: 'This folder contains files previously owned by Other User, and were transferred to you by your enterprise administrator. If you have any questions, please contact Enterprise Admin (admin@example.com).',
			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: '99999',
				name: 'Enterprise Admin',
				login: 'admin@example.com' },
			modified_by: 
			{ type: 'user',
				id: '99999',
				name: 'Enterprise Admin',
				login: 'admin@example.com' },
			trashed_at: null,
			purged_at: null,
			content_created_at: '2018-04-23T11:00:07-07:00',
			content_modified_at: '2018-04-23T11:00:07-07:00',
			owned_by: 
			{ type: 'user',
				id: '33333',
				name: 'Example User',
				login: 'user@example.com' },
			shared_link: null,
			folder_upload_email: null,
			parent: 
			{ type: 'folder',
				id: '0',
				sequence_id: null,
				etag: null,
				name: 'All Files' },
			item_status: 'active' }
		*/
	});
iOS
client.users.moveItemsOwnedByUser(withID: "33333", toUserWithID: "44444") { (result: Result<Folder, BoxSDKError>) in
    guard case let .success(folder) = result else {
        print("Error moving user content")
        return
    }

    print("Folder ID \(folder.id) created with migrated content")
}

Collaboration Transfer Method

The collaboration transfer method is a process that uses the collaboration endpoint to change the ownership of a single file or folder from one user to another instantaneously.

This method will perform an instantaneous transfer of ownership of a single file or folder, but cannot be used to transfer the root (all files and folders) from one user to another.

The general process, between transfer_from_user to transfer_to_user, will follow these steps:

Add Transfer To User as Co-Owner

The first step is to add the transfer_to_user account as a collaborator with co-owner access on the file or folder that should be transferred.

Making the call as the transfer_from_user account, add the transfer_to_user as a co-owner using the add collaboration endpoint.

cURL
curl -X POST https://api.box.com/2.0/collaborations \
     -H 'Authorization: Bearer <ACCESS_TOKEN>'
     -H 'Content-Type: application/json" '
     -d '{
       "item": {
         "type": "file",
         "id": "11446498"
       },
       "accessible_by": {
         "type": "user",
         "login": "123456"
       },
       "role": "editor"
     }'
.NET
// collaborate folder 11111 with user 22222
BoxCollaborationRequest requestParams = new BoxCollaborationRequest()
{
    Item = new BoxRequestEntity()
    {
        Type = BoxType.Folder,
        Id = "11111"
    },
    Role = "editor",
    AccessibleBy = new BoxCollaborationUserRequest()
    {
        Id = "22222"
    }
};
BoxCollaboration collab = await client.CollaborationsManager.AddCollaborationAsync(requestParams);
Java
BoxCollaborator user = new BoxUser(api, "user-id")
BoxFolder folder = new BoxFolder(api, "folder-id");
folder.collaborate(user, BoxCollaboration.Role.EDITOR);
Python
from boxsdk.object.collaboration import CollaborationRole

user = client.user(user_id='11111')
collaboration = client.folder(folder_id='22222').collaborate(user, CollaborationRole.VIEWER)

collaborator = collaboration.accessible_by
item = collaboration.item
has_accepted = 'has' if collaboration.status == 'accepted' else 'has not'
print('{0} {1} accepted the collaboration to folder "{2}"'.format(collaborator.name, has_accepted, item.name))
Node
// Invite user 123456 to collaborate on folder 987654
client.collaborations.createWithUserID('123456', '987654', client.collaborationRoles.EDITOR)
	.then(collaboration => {
		/* collaboration -> {
			type: 'collaboration',
			id: '11111',
			created_by: 
			{ type: 'user',
				id: '22222',
				name: 'Inviting User',
				login: 'inviter@example.com' },
			created_at: '2016-11-16T21:33:31-08:00',
			modified_at: '2016-11-16T21:33:31-08:00',
			expires_at: null,
			status: 'accepted',
			accessible_by: 
			{ type: 'user',
				id: '123456',
				name: 'Collaborator User',
				login: 'collaborator@example.com' },
			role: 'editor',
			acknowledged_at: '2016-11-16T21:33:31-08:00',
			item: 
			{ type: 'folder',
				id: '987654',
				sequence_id: '0',
				etag: '0',
				name: 'Collaborated Folder' } }
		*/
	});
iOS
client.collaborations.create(
    itemType: "folder",
    itemId: "22222",
    role: .editor,
    accessibleBy: "33333",
    accessibleByType: "user"
) { (result: Result<Collaboration, BoxSDKError>) in
    guard case let .success(collaboration) = result else {
        print("Error creating collaboration")
        return
    }

    print("Collaboration successfully created")
}

Fetch Collaboration ID as Transfer To User

The next step is make a request to get the collaboration information, making the request as the transfer_to_user account. The collaboration object returned will include a collaboration ID, which is used for the last step.

Making the call as the transfer_to_user account, get the collaboration on the file or folder ID being transferred, using the get collaboration endpoint. Capture the collaboration ID.

Remove Transfer From User as Owner

The final step is to remove the transfer_from_user account as an owner of the file or folder, which is accomplished using the delete collaboration endpoint.

Making call as the transfer_to_user account, remove the transfer_from_user as a collaborator on the file or folder.

The file or folder is now owned by the transfer_to_user account, and the transfer_from_user account no longer has access.