Collections contain information about the items contained inside of them, including files and folders. The only collection available currently is a “Favorites” collection. The contents of the collection are discovered in a similar way in which the contents of a folder are discovered.
Existing collections can be retrieved by calling the
getAllCollections(BoxAPIConnection)
method. Currently only
"Favorites" collection is supported.
Iterable<BoxCollection.Info> collections = BoxCollection.getAllCollections(api);
for (BoxCollection.Info collectionInfo : collections) {
// Do something with the collection.
}
Every BoxCollection
implements Iterable<BoxItem>
which allows
you to iterate over the collection's contents. The iterator automatically
handles paging and will make additional network calls to load more data from Box
when necessary.
BoxFolder folder = new BoxFolder(api, "id");
for (BoxItem.Info itemInfo : folder) {
if (itemInfo instanceof BoxFile.Info) {
BoxFile.Info fileInfo = (BoxFile.Info) itemInfo;
// Do something with the file.
} else if (itemInfo instanceof BoxFolder.Info) {
BoxFolder.Info folderInfo = (BoxFolder.Info) itemInfo;
// Do something with the folder.
}
}
Add an item to a collection by calling
setCollections(BoxCollection... collections)
on any BoxItem
. Note that this
method will overwrite all collections that the item belongs to.
BoxCollection favorites = null;
for (BoxCollection.Info info : BoxCollection.getAllCollections(api)) {
if (info.getCollectionType().equals("favorites")) {
favorites = info.getResource();
break;
}
}
BoxFile file = new BoxFile(api, "id");
file.setCollections(favorites);
Remove an item from a collection by calling
setCollections(BoxCollection... collections)
on any BoxItem
and exclude the
collection to wish to remove it from.
BoxFile file = new BoxFile(api, "id");
BoxFile.Info info = file.getInfo("collections");
ArrayList<BoxCollection> collections = new ArrayList<BoxCollection>();
for (BoxCollection.Info info : info.getCollections(api)) {
// Include every existing collection except for favorites to remove the file
// from the favorites collection.
if (!info.getCollectionType().equals("favorites")) {
collections.add(info.getResource());
}
}
file.setCollections(collections.toArray());