Skip to content

Latest commit

 

History

History
100 lines (79 loc) · 3.47 KB

collections.md

File metadata and controls

100 lines (79 loc) · 3.47 KB

Collections

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.

Get Collections

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

Get a Collection's Items

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 Items to a Collection

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 Items from a Collection

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());