---
title: "Media processing script"
slug: "media-processing-script"
abstract: "Web script example that shows how to extract metadata properties from the context during asset processing and add them to the asset's metadata."
---
# Media processing script

This is an example of a *Media processing* web script, which is executed every time an asset is processed by the respective worker (for example, when a new asset is uploaded or when an asset rendition is refreshed). It extracts the metadata properties from the context and adds them to the asset.

## Script

```csharp
using System.Linq;

var masterFileRelation = await Context.File.GetRelationAsync<IChildToManyParentsRelation>("MasterFile");

if (!masterFileRelation.Parents.Any() || !masterFileRelation.Parents.Contains(Context.Asset.Id.Value))
{
  return;
}

string ToCsvValue(object source)
{
  var str = source.ToString();
  if (str.Contains(","))
  {
    return "\"" + str + "\"";
  }
  return str;
}

var headers = string.Join(", ", Context.MetadataProperties.Keys.Select(ToCsvValue));
var values = string.Join(", ", Context.MetadataProperties.Values.Select(ToCsvValue));

var metadataProp = await Context.Asset.GetPropertyAsync<ICultureInsensitiveProperty>("Metadata");
metadataProp.SetValue(headers + "\n" + values);

await MClient.Entities.SaveAsync(Context.Asset);

```

## Script explanation

This section steps through the script in execution order, explaining each part. The numbered items describe the sequence, not instructions to perform.

1. Include the libraries to be used in the script.

    ```csharp
    using System.Linq;
    ```

2. Get the `MasterFile` relation of the asset.

    ```csharp
    var masterFileRelation = await Context.File.GetRelationAsync<IChildToManyParentsRelation>("MasterFile");
    ```

3. Check if the current file is the master file. Only this metadata is to be copied if this is the case.

    ```csharp
    if (!masterFileRelation.Parents.Any() || !masterFileRelation.Parents.Contains(Context.Asset.Id.Value))
    {
      return;
    }
    ```

4. Create a method that checks whether the property already has commas and escapes them with quotation marks.

    ```csharp
    string ToCsvValue(object source)
    {
      var str = source.ToString();
      if (str.Contains(","))
      {
        return "\"" + str + "\"";
      }
      return str;
    }
    ```

5. Convert the metadata headers to csv.

    ```csharp
    var headers = string.Join(", ", Context.MetadataProperties.Keys.Select(ToCsvValue));
    ```

6. Convert the metadata values to csv.

    ```csharp
    var values = string.Join(", ", Context.MetadataProperties.Values.Select(ToCsvValue));
    ```

7. Get the metadata property field of the asset.

    ```csharp
    var metadataProp = await Context.Asset.GetPropertyAsync<ICultureInsensitiveProperty>("Metadata");
    ```

8. Store the metadata on the asset.

    ```csharp
    metadataProp.SetValue(headers + "\n" + values);
    ```

9. Save the asset.

    ```csharp
    await MClient.Entities.SaveAsync(Context.Asset);
    ```

## Setup

1. [Create a string property](https://doc.sitecore.com/ch/en/users/content-hub/create-a-member.html) called `Metadata` on the `M.Asset` definition, setting the `content type` to `Multiple lines`.
2. [Create](https://doc.sitecore.com/ch/en/developers/cloud-dev/work-with-scripts.html "Work with scripts"), publish, and enable a *metadata processing* script.