1

We have a situation where we have 2 users, UserA and UserB. I want to transfer all the files and folders of UserB to UserA.

We are using collaborations API to perform this task. But getting following error

{"type":"error","status":404,"code":"not_found","context_info":{"errors":[{"reason":"invalid_parameter","name":"role","message":"Invalid value 'Owner'. 'role' with value 'Owner' not found"}]},"help_url":"http://developers.box.com/docs/#errors","message":"Not Found","request_id":"304354248562a743910984"}

I get a valid response when I pass anything apart from "owner" as role in request. I don't know what is wrong with "owner" role.

Here is the request

POST https://api.box.com/2.0/collaborations/ HTTP/1.1 
As-User: 254598270 
Authorization: Bearer UPSUE228kXGuDBGzy07G5fGxIrDL1QDj 
Content-Type: text/plain; charset=utf-8 
Host: api.box.com 
Content-Length: 108 
Expect: 100-continue 
Accept-Encoding: gzip, deflate
{"item":{"id":"5092905410","type":"folder"},"accessible_by":{"id":"254608030","type":"user"},"role":"owner"}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Abhishek
  • 621
  • 1
  • 8
  • 19

2 Answers2

4

I think the documentation might be wrong. Specifying an "owner" role in a collaboration doesn't really make sense given how collaborations work.

However, the move user's folder endpoint might be what you're looking for. It allows you to move the entire root folder of one user into another user's account. Here's an example cURL request:

curl https://api.box.com/2.0/users/USER_ID/folders/FOLDER_ID \
    -H "Authorization: Bearer ACCESS_TOKEN" \
    -d '{"owned_by": {"id": "USER_ID"}}' \
    -X PUT

Note that only the root folder is supported right now (which is folder ID 0). Fortunately, it looks like that's the folder you're trying to move.

Greg
  • 3,731
  • 1
  • 29
  • 25
  • However, you can use collaboration.update_info (python sdk) (there must be a collaboration existing before you update the role to 'owner'). source: https://community.box.com/t5/Platform-and-Development-Forum/Updating-owned-by-property-has-no-effect/td-p/60394 – Shmuel Kamensky May 24 '19 at 20:22
  • only the root folder is supported right now, means, sub folder is not transfered? – Amay Kulkarni Jul 28 '21 at 14:17
0

All that's missing is that you must add them as an editor prior to making them an owner.

If you follow https://developer.box.com/guides/folders/single/change-owner/

You will see that transferring ownership actually doesn't require a folder move. It is a very simple two (or three) step process:

  1. Add the new user as a collaborator of role EDITOR
  2. Immediately update that collaboration to the role of OWNER

That's it. Optionally, you now can remove the collaboration of the original owner, who has been downgraded from owner to editor.

Here is my example using the Python SDK:

bc = box.BoxClient()  # This is my own client wrapper
owner = bc.get_user(old_owner.lastname)
user_client = bc.get_user_client(owner.id)  # Get a user_client for the owner
src_folder = user_client.folder(FOLDER_ID)

target = bc.get_user('new_owner.lastname')

new_collab = src_folder.collaborate(target, CollaborationRole.EDITOR)
updated_collaboration = new_collab.update_info(CollaborationRole.OWNER)

# Now we can remove the original owner as collaborator:
for collab in src_folder.get_collaborations():
    target = collab.accessible_by
    if target.id == owner.id:
        collab.delete()
supermitch
  • 2,062
  • 4
  • 22
  • 28