メタデータテンプレートの作成
メタデータテンプレートの作成
メタデータテンプレートを作成するには、scope
、displayName
、および必要に応じて一連のfields
をPOST /metadata_templates/schema
APIに渡します。
curl -i -X POST "https://api.box.com/2.0/metadata_templates/schema" \
-H "authorization: Bearer <ACCESS_TOKEN>" \
-H "content-type: application/json" \
-d '{
"scope": "enterprise",
"displayName": "Customer",
"fields": [
{
"type": "string",
"key": "name",
"displayName": "Name",
"description": "The customer name",
"hidden": false
},
{
"type": "date",
"key": "last_contacted_at",
"displayName": "Last Contacted At",
"description": "When this customer was last contacted at",
"hidden": false
},
{
"type": "enum",
"key": "industry",
"displayName": "Industry",
"options": [
{"key": "Technology"},
{"key": "Healthcare"},
{"key": "Legal"}
]
},
{
"type": "multiSelect",
"key": "role",
"displayName": "Contact Role",
"options": [
{"key": "Developer"},
{"key": "Business Owner"},
{"key": "Marketing"},
{"key": "Legal"},
{"key": "Sales"}
]
}
]
}'
await client.metadataTemplates.createMetadataTemplate({
scope: 'enterprise',
displayName: templateKey,
templateKey: templateKey,
fields: [
{
key: 'firstName',
displayName: 'First name',
description: 'Person first name',
type: 'string' as CreateMetadataTemplateRequestBodyFieldsTypeField,
} satisfies CreateMetadataTemplateRequestBodyFieldsField,
{
key: 'lastName',
displayName: 'Last name',
description: 'Person last name',
type: 'string' as CreateMetadataTemplateRequestBodyFieldsTypeField,
} satisfies CreateMetadataTemplateRequestBodyFieldsField,
{
key: 'dateOfBirth',
displayName: 'Birth date',
description: 'Person date of birth',
type: 'date' as CreateMetadataTemplateRequestBodyFieldsTypeField,
} satisfies CreateMetadataTemplateRequestBodyFieldsField,
{
key: 'age',
displayName: 'Age',
description: 'Person age',
type: 'float' as CreateMetadataTemplateRequestBodyFieldsTypeField,
} satisfies CreateMetadataTemplateRequestBodyFieldsField,
{
key: 'hobby',
displayName: 'Hobby',
description: 'Person hobby',
type: 'multiSelect' as CreateMetadataTemplateRequestBodyFieldsTypeField,
} satisfies CreateMetadataTemplateRequestBodyFieldsField,
],
} satisfies CreateMetadataTemplateRequestBody);
client.metadata_templates.create_metadata_template(
"enterprise",
template_key,
template_key=template_key,
fields=[
CreateMetadataTemplateFields(
key="firstName",
display_name="First name",
description="Person first name",
type=CreateMetadataTemplateFieldsTypeField.STRING.value,
),
CreateMetadataTemplateFields(
key="lastName",
display_name="Last name",
description="Person last name",
type=CreateMetadataTemplateFieldsTypeField.STRING.value,
),
CreateMetadataTemplateFields(
key="dateOfBirth",
display_name="Birth date",
description="Person date of birth",
type=CreateMetadataTemplateFieldsTypeField.DATE.value,
),
CreateMetadataTemplateFields(
key="age",
display_name="Age",
description="Person age",
type=CreateMetadataTemplateFieldsTypeField.FLOAT.value,
),
CreateMetadataTemplateFields(
key="hobby",
display_name="Hobby",
description="Person hobby",
type=CreateMetadataTemplateFieldsTypeField.MULTISELECT.value,
),
],
)
await client.MetadataTemplates.CreateMetadataTemplateAsync(requestBody: new CreateMetadataTemplateRequestBody(scope: "enterprise", displayName: templateKey) { TemplateKey = templateKey, Fields = Array.AsReadOnly(new [] {new CreateMetadataTemplateRequestBodyFieldsField(key: "firstName", displayName: "First name", type: CreateMetadataTemplateRequestBodyFieldsTypeField.String) { Description = "Person first name" },new CreateMetadataTemplateRequestBodyFieldsField(key: "lastName", displayName: "Last name", type: CreateMetadataTemplateRequestBodyFieldsTypeField.String) { Description = "Person last name" },new CreateMetadataTemplateRequestBodyFieldsField(key: "dateOfBirth", displayName: "Birth date", type: CreateMetadataTemplateRequestBodyFieldsTypeField.Date) { Description = "Person date of birth" },new CreateMetadataTemplateRequestBodyFieldsField(key: "age", displayName: "Age", type: CreateMetadataTemplateRequestBodyFieldsTypeField.Float) { Description = "Person age" },new CreateMetadataTemplateRequestBodyFieldsField(key: "hobby", displayName: "Hobby", type: CreateMetadataTemplateRequestBodyFieldsTypeField.MultiSelect) { Description = "Person hobby" }}) });
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);
from boxsdk.object.metadata_template import MetadataField, MetadataFieldType
fields = [
MetadataField(MetadataFieldType.STRING, 'Name'),
MetadataField(MetadataFieldType.DATE, 'Birthday', 'bday'),
MetadataField(MetadataFieldType.ENUM, 'State', options=['CA', 'TX', 'NY'])
]
template = client.create_metadata_template('Employee Record', fields, hidden=True)
print(f'Metadata template ID {template.scope}/{template.templateKey} created!')
var templateParams = new BoxMetadataTemplate()
{
TemplateKey = "marketingCollateral",
DisplayName = "Marketing Collateral",
Scope = "enterprise",
Fields = new List<BoxMetadataTemplateField>()
{
new BoxMetadataTemplateField()
{
Type = "enum",
Key = "audience",
DisplayName = "Audience",
Options = new List<BoxMetadataTemplateFieldOption>()
{
new BoxMetadataTemplateFieldOption() { Key = "internal" },
new BoxMetadataTemplateFieldOption() { Key = "external" }
}
},
new BoxMetadataTemplateField()
{
Type = "string",
Key = "author",
DisplayName = "Author"
}
}
};
BoxMetadataTemplate template = await client.MetadataManager.CreateMetadataTemplate(templateParams);
// Create a new template, but hide it for now until it's ready for use
client.metadata.createTemplate(
'Vendor Contract',
[
{
type: 'date',
key: 'signed',
displayName: 'Date Signed'
},
{
type: 'string',
key: 'vendor',
displayName: 'Vendor'
},
{
type: 'enum',
key: 'fy',
displayName: 'Fiscal Year',
options: [
{key: 'FY17'},
{key: 'FY18'},
{key: 'FY19'}
]
}
],
{
hidden: true,
templateKey: 'vcontract',
copyInstanceOnItemCopy: false
}
)
.then(template => {
/* template -> {
id: '17f2d715-6acb-45f2-b96a-28b15efc9faa',
templateKey: 'vcontract',
scope: 'enterprise_12345',
displayName: 'Vendor Contract',
hidden: true,
copyInstanceOnItemCopy: false,
fields:
[ { type: 'date',
key: 'signed',
displayName: 'Date Signed',
hidden: false },
{ type: 'string',
key: 'vendor',
displayName: 'Vendor',
hidden: false },
{ type: 'enum',
key: 'fy',
displayName: 'Fiscal Year',
options:
[ { key: 'FY17' },
{ key: 'FY18' },
{ key: 'FY19' } ],
hidden: false } ] }
*/
});
var templateFields: [MetadataField] = []
templateFields.append(MetadataField(
type: "string",
key: "name",
displayName: "Full Name"
))
templateFields.append(MetadataField(
type: "date",
key: "birthday",
displayName: "Birthday"
))
templateFields.append(MetadataField(
type: "enum",
key: "department",
displayName: "Department",
options: [
["key": "HR"],
["key": "Sales"],
["key": "Marketing"],
]
))
client.metadata.createTemplate(
scope: "enterprise",
templateKey: "personnelRecord",
displayName: "Personnel Record",
hidden: false,
fields: templateFields
) { (result: Result<MetadataTemplate, BoxSDKError>) in
guard case let .success(template) = result {
print("Error creating metadata template")
return
}
print("Created metadata template with ID \(template.id)")
}
テンプレートフィールド
fields
属性は、ユーザーがテンプレートで入力できる個々のフィールドのセットを表します。たとえば、customer
テンプレートにstring
タイプのname
フィールドがあるとします。
テンプレ ートフィールドは、string
、enum
、float
、date
、enum
、またはmultiSelect
タイプのいずれかになります。各フィールドには少なくともtype
、displayName
、およびkey
が必要です。
{
"scope": "enterprise",
"displayName": "Customer",
"fields": [
{
"type": "string",
"key": "name",
"displayName": "Name"
}
]
}
enum
およびmultiSelect
フィールドタイプは、ユーザーが項目のリストからそれぞれ1つまたは複数のオプションを選択できるドロップダウンリストを表します。
{
"scope": "enterprise",
"displayName": "Customer",
"fields": [
{
"type": "enum",
"key": "industry",
"displayName": "Industry",
"options": [
{"key": "Technology"},
{"key": "Healthcare"},
{"key": "Legal"}
]
}
]
}
メタデータテンプレートフィールドの詳細を確認する
テンプレートキー
メタデータテンプレートが作成されると、templateKey
を明示的に指定した場合を除き、テンプレートのdisplayName
から自動的にtemplateKey
が生成されます。テンプレートキーを作成する際に、名前に含まれるスペースと規格外の文字は削除され、文字列はキャメルケースに変換されます。
たとえば、Test Name (with-special_) Characters
という名前のメタデータテンプレートのtemplateKey
はtestNameWithspecialCharacters
になります。
その後、このテンプレートキーは、テンプレートの情報を取得したり、項目にテンプレートを割り当てたりするためのAPIリクエストを実行するときに使用されます。