Metadata that belongs to a file is grouped by templates. Templates allow the metadata service to provide a multitude of services, such as pre-defining sets of key:value pairs or schema enforcement on specific fields.
- Create Metadata Template
- Update Metadata Template
- Get Metadata Template
- Get Enterprise Metadata Templates
- Delete a Metadata Template
- Execute Metadata Query
The createMetadataTemplate(BoxAPIConnection api, String templateScope, String templateKey, String displayName, Boolean hidden, List<Field> fields)
method will create a metadata template schema.
You can create custom metadata fields that will be associated with the metadata template.
MetadataTemplate.Field metadataField = new MetadataTemplate.Field();
metadataField.setType("string");
metadataField.setKey("text");
metadataField.setDisplayName("Text");
List<MetadataTemplate.Field> fields = new ArrayList<MetadataTemplate.Field>();
fields.add(metadataField);
MetadataTemplate template = MetadataTemplate.createMetadataTemplate(api, "enterprise", "CustomField", "Custom Field", false, fields);
final JsonObject jsonObject = new JsonObject();
jsonObject.add("text", "This is a test text");
Metadata metadata = new Metadata(jsonObject);
boxFile.createMetadata("CustomField", metadata);
To update an existing metadata template, call the
updateMetadataTemplate(BoxAPIConnection api, String scope, String template, List<FieldOperation> fieldOperations)
method with the scope and key of the template, and the list of field operations to perform:
List<MetadataTemplate.FieldOperation> updates = new ArrayList<MetadataTemplate.FieldOperation>();
String addCategoryFieldJSON = "{\"op\":\"addField\","\"data\":{"
+ "\"displayName\":\"Category\",\"key\":\"category\",\"hidden\":false,\"type\":\"string\"}}";
updates.add(new MetadataTemplate.FieldOperation(addCategoryFieldJSON));
String changeTemplateNameJSON = "{\"op\":\"editTemplate\",\"data\":{"
+ "\"displayName\":\"My Metadata\"}}";
updates.add(new MetadataTemplate.FieldOperation(changeTemplateNameJSON));
MetadataTemplate.updateMetadataTemplate(api, "enterprise", "myData", updates);
The getMetadataTemplate(BoxAPIConnection api)
method will return information about default metadata schema. Also,
getMetadataTemplate(BoxAPIConnection api, String templateKey)
and
getMetadataTemplate(BoxAPIConnection api, String templateKey, String templateScope, String... fields)
can be used to set metadata template name, metadata scope and fields to retrieve.
MetadataTemplate template = MetadataTemplate.getMetadataTemplate(api, "templateName");
The static MetadataTemplate.getMetadataTemplateByID(BoxAPIConnection api, String templateID)
method will return a specific metadata template.
MetadataTemplate template = MetadataTemplate.getMetadataTemplateByID(api, "37c0204b-3fe1-4a32-b9da-f28e88f4c4c6");
Calling the static
getEnterpriseMetadataTemplates(BoxAPIConnection api, String... fields)
will return an iterable that will page through all metadata templates within a user's enterprise.
Also, getEnterpriseMetadataTemplates(String templateScope, BoxAPIConnection api, String... fields)
and
getEnterpriseMetadataTemplates(String templateScope, int limit, BoxAPIConnection api, String... fields)
can be used to set metadata scope, limit of items per single response.
Iterable<MetadataTemplate> templates = MetadataTemplate.getEnterpriseMetadataTemplates(api);
for (MetadataTemplate templateInfo : templates) {
// Do something with the metadata template.
}
To return the metadata templates available to all enterprises pass in the
global
scope.
Iterable<MetadataTemplate> templates = MetadataTemplate.getEnterpriseMetadataTemplates('global', api);
for (MetadataTemplate templateInfo : templates) {
// Do something with the metadata template.
}
The deleteMetadataTemplate(BoxAPIConnection api, String scope, String template)
method will remove a metadata template schema from an enterprise.
MetadataTemplate.deleteMetadataTemplate(api, "enterprise", "templateName");
The executeMetadataQuery(BoxAPIConnection api, MetadataQuery queryBody)
method queries files and folders based on their metadata.
String from = "enterprise_341532.test";
String query = "testfield = :arg";
String ancestorFolderId = "0";
MetadataQuery.OrderBy primaryOrderBy = MetadataQuery.OrderBy.ascending("primarySortKey");
MetadataQuery.OrderBy secondaryOrderBy = MetadataQuery.OrderBy.ascending("secondarySortKey");
MetadataQuery mQuery = new MetadataQuery(from);
mQuery.setQuery(query);
mQuery.setAncestorFolderId(ancestorFolderId);
mQuery.setOrderBy(primaryOrderBy, secondaryOrderBy);
mQuery.addParameter("arg", "test");
mQuery.setFields("metadata.enterprise_341532.test.customField");
BoxResourceIterable<BoxItem.Info> results = MetadataTemplate.executeMetadataQuery(api, mQuery);
for (BoxItem.Info r: results) {
if (r instanceof BoxFile.Info) {
BoxFile.Info fileInfo = (BoxFile.Info) r;
Metadata fileMetadata = fileInfo.getMetadata("test", "enterprise_341532");
String customFieldValue = fileMetadata.getString("/customField");
System.out.println(customFieldValue);
} else if (r instanceof BoxFolder.Info) {
BoxFolder.Info folderInfo = (BoxFolder.Info) r;
Metadata folderMetadata = folderInfo.getMetadata("test", "enterprise_341532");
String customFieldValue = folderMetadata.getString("/customField");
System.out.println(customFieldValue);
}
}