Collections allow users to mark specific files, folders and web links to make it easier to find them.
- Get a User's Collections
- Get the Items in a Collection
- Add an Item to a Collection
- Remove an Item from a Collection
To get all collections belonging to a user, call client.collections(limit=None, offset=0, fields=None)
.
This method returns a BoxObjectCollection
that you can use to iterate over all the
Collection
objects in the set.
collections = client.collections()
for collection in collections:
print(f'Collection "{collection.name}" has ID {collection.id}')
To retrieve a list of items contained in a collection, call
collection.get_items(limit=None, offset=0, fields=None)
. This method returns a
BoxObjectCollection
that you can use to iterate over all the BaseItem
objects in
the collection. BaseItem
is a super class for files, folders and web links.
items = client.collection(collection_id='12345').get_items()
for item in items:
print(f'{item.type.capitalize()} "{item.name}" is in the collection')
To add an BaseItem
to a collection, call item.add_to_collection(collection)
with the
Collection
you want to add the item to. This method returns the updated BaseItem
object.
collection = client.collection(collection_id='12345')
updated_file = client.file(file_id='11111').add_to_collection(collection)
print(f'File "{updated_file.name}" added to collection!')
To remove an BaseItem
from a collection, call
item.remove_from_collection(collection)
with the Collection
you want
to remove the item from. This method returns the updated BaseItem
object.
collection = client.collection(collection_id='12345')
updated_file = client.file(file_id='11111').remove_from_collection(collection)
print(f'File "{updated_file.name}" removed from collection!')