Skip to main content
Question

Assign Metadata Template created in admin console to files - Java SDK

  • May 21, 2025
  • 2 replies
  • 53 views

Forum|alt.badge.img
Hi at all, I'm trying to assign to a file in my Box account an existing Metadata Template that I have created with admin console. Throught API get Metadata Template call, I've saved the JSON response with all information in the schema ( such as templateKey, scope, displayName, fields[] etc...). 

I've created a MetadataTemplate object with the JSON and try to create a Metadata on a file based to MetadataTemplate, but box sdk return me error code 400.

This is my example code, I don't know where I make mistake =( :

 

// read the json file
FileReader reader = new FileReader("path/to/file.json");

JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

// create template based on JSON stringified
MetadataTemplate imgTemplate = new MetadataTemplate(jsonObject.toJSONString());

// create custom metadata values to add on template
Metadata assignMeta = new Metadata();

assignMeta.add( imgTemplate.getFields().get(0).getKey(),"test");
assignMeta.add( imgTemplate.getFields().get(1).getKey(),"10");
assignMeta.add( imgTemplate.getFields().get(2).getKey(),"png");

// assign template to this file with custom metadata
file.createMetadata(imgTemplate.getTemplateKey(), imgTemplate.getScope(), assignMeta);

 

Someone can help me please???? I've just tried all in my head...

2 replies

Forum|alt.badge.img

 Can you please share the error message that the API is returning along with the error code? 


Forum|alt.badge.img

I have resolved, thanks you the same 😃

 

1) Create a MetadataTemplate from a copy based on a pre-existing schema (defined from admin console):

FileReader reader = new FileReader("path/to/schema.json");

JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

// create MetadataTemplate based on JSON stringified
MetadataTemplate imgTemplate = new MetadataTemplate(jsonObject.toJSONString());

 

2) Create a NEW Metadata with the correct couple key:values (attention on types of key):

Metadata metadata = new Metadata().add("/key1", "valu1").add("/key2", "value2");

 

3) Assign template to file you want to with custom metadata

//file.createMetadata(imgTemplate.getTemplateKey(), imgTemplate.getScope(), metadata);