# Manage metadata

> Manage asset metadata in Unity Asset Manager using the Python SDK, including how to create, update, and delete metadata fields.

## Before you begin

To access any metadata from an asset, dataset or file, make sure the app has all the right accesses. Read more about [managing Identity](/asset-manager/python-sdk/manage-identity.md).

> **Note:**
>
> The following code snippets first require an initialization. To learn more about the `initialize()` and `uninitialize()` methods, read about [getting started with the Unity Cloud Python SDK](/asset-manager/python-sdk/get-started.md).

## How do I...?

### Create a metadata field definition

To create a metadata field, follow these steps:

1. Create a `CreateField` object and fill in the necessary information.
2. Call `create_field_definition` with the organization id and your `CreateField` object.
   The possible for `FieldDefinitionType `are:
   * `BOOLEAN`
   * `SELECTION`
   * `NUMBER`
   * `TEXT`
   * `TIMESTAMP`
   * `URL`
   * `USER`

> **Note:**
>
> The `accepted_values` and `multiselection` properties are only necessary when creating a `SELECT `metadata field. All other type will ignore those properties.

```python
create_field = CreateFieldDefinition(
  key = "my_new_field",
  display_name = "My Field",
  type = unity_cloud.assets.FieldDefinitionType.SELECT,
  accepted_values = ["value1", "value2", "value3"],
  multiselection = False
)

unity_cloud.assets.create_field_definition(org_id = "0123456", create_field = create_field)
```

### Update a metadata field definition

To update a metadata field, follow these steps:

1. Create an `UpdateField` object along with the information to update.
2. Call `update_field_definition` with the organization id and your `UpdateFieldobject`.

```python
update_field = UpdateFieldDefinition(
  key = "my_new_field",
  display_name = "My Field with a new name",
  accepted_values = ["value1", "value2", "value3", "value4"]
)

unity_cloud.assets.update_field_definition(org_id = "0123456", update_field = create_field)
```

### Add accepted values to a SELECT metadata field

To add accepted values to a metadata field of type `SELECT`, call `add_accepted_values_to_field_definition` with the new values.

This action removes the necessity of calling `update_field_definition` with all the already existing values.

```python

unity_cloud.assets.add_accepted_values_to_field_definition(org_id = "0123456", field_key = "my_new_field", accepted_values = ["value5", "value6"])
```

### Remove accepted values from a SELECT metadata field

To remove accepted values to a metadata field of type `SELECT`, call `remove_accepted_values_from_field_definition` with the values you want removed.

This action removes the necessity of calling `update_field_definition` with the values you want to keep.

```python

unity_cloud.assets.remove_accepted_values_from_field_definition(org_id = "0123456", field_key = "my_new_field", accepted_values = ["value5", "value6"])
```

### Delete a metadata field definition

To delete a metadata field, call delete\_field\_definition with its organization id and field key.

> **Note:**
>
> A deleted field will still be retrievable later, but you cannot undelete it.

```python
unity_cloud.assets.delete_field_definition(org_id = "0123456", field_key = "my_new_field")
```

### List all field definitions

To retrieve all metadata field from an organization, call `list_field_definitions` with the organization id and whether or not you want deleted field included in the list.

* (Optional) Add the parameter name\_sorting\_order to sort the results alphabetically either in ascending or descending.
* (Optional) Add the parameter `limit_to` to limit the amount of results returned. Leave it unchanged or set it to 0 to return everything.
* (Optional) Add the parameter `skip` to specify the number of collection to skip in the results. Use in combination with limit\_to to page through results.

```python
fields = unity_cloud.assets.list_field_definitions(
  org_id = "0123456",
  include_deleted = False,
  name_sorting_order = SortingOrder.ASCENDING,
  limit_to = 10,
  skip = 0)
```

### Get a field definition

To retrieve a metadata field from an organization, call `get_field_definition` with the organization id and the key of the field.

```python
field = unity_cloud.assets.get_field_definition(org_id = "0123456", field_key = "my_new_field")
```

### Manage metadata

When managing `Metadata` dictionaries, make sure the dictionaries have existing metadata field keys and consistent values according to their type.

For a field with a `SELECT` type, accepted\_values can either be a string or an array of strings. This distinction depends on the value set for multiselection:

* If set to `TRUE`, the `accepted_values` is an array of strings.
* If set to `FALSE`, the `accepted_values` is a string.

> **Note:**
>
> The following code snippets will manage metadata from an asset, but the same can be done either for metadata from files and datasets.

* Use the following code sample to create an entity:

```python
metadata = dict()
metadata["Language"] = "C#"

asset_creation = AssetCreation(
  name = "your-asset-name",
  description = "your-asset-description",
  type = unity_cloud.assets.asset_type.MODEL_3D,
  metadata = metadata
)
```

* Use the following code sample to update an entity:

```python
    metadata = dict()
    metadata["Language"] = "C#"

    asset_update = AssetUpdate(
      name = "your-asset-name",
      metadata = metadata
    )
```

* Use the following code sample to fetch an entity's metadata:

```python
asset_metadata = unity_cloud.assets.get_asset_metadata(
  org_id = "012345",
  project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
  asset_id = "0123456789",
  keys = [])

asset_prog_language = asset_metadata["Language"]

dataset_metadata = unity_cloud.datasets.get_dataset_metadata(
  org_id = "012345",
  project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
  asset_id = "0123456789",
  dataset_id = "0123456789",
  keys = [])

file_Metadata = unity_cloud.files.get_file_metadata(
  org_id = "012345",
  project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
  asset_id = "0123456789",
  dataset_id = "0123456789",
  cloud_file_path = PurePosixPath("path/to/file.ext"),
  keys = [])
```

> **Note:**
>
> Leave the `keys` parameter empty to retrieve all metadata fields. Otherwise, specify the keys you want to retrieve.
