---
title: "Create public links for approved assets automatically"
slug: "create-public-links-for-approved-assets-automatically"
abstract: "How to use a script, an action, and a trigger to automate the creation of public links for approved assets."
---
# Create public links for approved assets automatically

When you have a lot of assets and need to create public links for all of them after they are approved, it's easier and faster to use Sitecore Content Hub's actions and triggers. This way, a public link is created automatically as soon as an asset is approved.

This walkthrough describes how you can automate the creation of public links and describes how to:

- [Create a script](https://doc.sitecore.com/ch/en/developers/cloud-dev/create-public-links-for-approved-assets-automatically.html#UUID-7b6bb34b-da8a-64a0-8d48-d3e7f7eef204_N1715005436031 "Create a script")
- [Create an action](https://doc.sitecore.com/ch/en/developers/cloud-dev/create-public-links-for-approved-assets-automatically.html#UUID-7b6bb34b-da8a-64a0-8d48-d3e7f7eef204_N1715005449688 "Create an action")
- [Create a trigger](https://doc.sitecore.com/ch/en/developers/cloud-dev/create-public-links-for-approved-assets-automatically.html#UUID-7b6bb34b-da8a-64a0-8d48-d3e7f7eef204_N1715005464600 "Create a trigger")

## Create a script

The first step is to create a [script](https://doc.sitecore.com/ch/en/developers/cloud-dev/scripts.html "Scripts") that tells Content Hub to automate the creation of public links for the original asset renditions if the links don't already exist.

To create a script:

1. On the menu bar, click **Manage** :mdiCogOutline:.
2. On the **Manage** page, click **Scripts**.
3. On the **Scripts** page, click :mdiPlus: **Script**.
4. In the **Script** dialog, fill in the following fields:

    | Field | Description |
    | --- | --- |
    | **Name** | A name for your script, for example, *CreateAssetPublicLinkOnApproval*. |
    | **Type** | A script type, for example, *Action*. |
    | **Description** | An optional description for your script. |

5. Click **Save**.
6. On the **Scripts** page, click the *CreateAssetPublicLinkOnApproval* script you created.
7. On the script details page, in the top-right corner, click **Edit**.
8. In the script editor, enter the following code:

    ```csharp
    using System.Linq;
    using System.Threading.Tasks;

    var assetId = Context.TargetId;

    // Check if public links don't exist yet
    var query = Query.CreateQuery(entities => from e in entities
      where e.DefinitionName == "M.PublicLink"
      && e.Parent("AssetToPublicLink") == assetId.Value
      && e.Property("Resource") == "downloadOriginal"
      && e.Property("IsDisabled") == false
      select e);
    query.Take = 1;

    var result = await MClient.Querying.QueryIdsAsync(query);
    if (result.TotalNumberOfResults > 0)
    {
      MClient.Logger.Info("Public links already exist for asset with id '" + assetId + "'");
      return;
    }

    // Create public links
    await CreateForRendition("downloadOriginal", assetId.Value);
    MClient.Logger.Info("Created public link 'downloadOriginal' for asset with id '" + assetId + "'");

    async Task CreateForRendition(string rendition, long assetId)
    {
      var publicLink = await MClient.EntityFactory.CreateAsync("M.PublicLink");

      if (publicLink.CanDoLazyLoading())
      {
        await publicLink.LoadMembersAsync(new PropertyLoadOption("Resource"), new RelationLoadOption("AssetToPublicLink"));
      }

      publicLink.SetPropertyValue("Resource", rendition);

      var relation = publicLink.GetRelation<IChildToManyParentsRelation>("AssetToPublicLink");
      if (relation == null)
      {
        MClient.Logger.Error("Unable to create public link: no AssetToPublicLink relation found.");
        return;
      }

      relation.Parents.Add(assetId);

      await MClient.Entities.SaveAsync(publicLink);
    }
    ```

9. Click **Save changes**.
10. Click **Build**.
11. Click **Publish**.

    ![The script details page.](https://doc.sitecore.com/api/markdown-image/ch/en/developers/cloud-dev/images/step_one_-_create_a_script.png)

## Create an action

The next step is to create an [action](https://doc.sitecore.com/ch/en/developers/cloud-dev/create-an-action.html "Create an action") based on the *CreateAssetPublicLinkOnApproval* script.

To create an action:

1. On the **Manage** page, click **Actions**.
2. On the **Actions** page, click **New action**.
3. In the **New action** dialog, fill in the following fields:

    | Field | Description |
    | --- | --- |
    | **Name** | A name for your action. You can use the script name, namely *CreateAssetPublicLinkOnApproval*. |
    | **Label** | Optional label for your action. |
    | **Type** | A type for this action. For this example, select **Action script**. |
    | **Script** | The script you want to use, for example, *CreateAssetPublicLinkOnApproval*. |

4. Click **Save**.

## Create a trigger

The last step is to create a trigger that will create a public link every time an asset is approved.

To create a trigger:

1. On the **Manage** page, click **Triggers**.
2. On the **Triggers** page, click **New trigger**.
3. On the trigger creation page, on the **General** tab, fill in the following fields:

    | Field | Description |
    | --- | --- |
    | **Name** | The name of the trigger, for example, *CreateAssetPublicLinkOnApproval*. |
    | **Description** | Optionally, enter a label for your action. |
    | **Objective** | Select **Entity creation** and **Entity modification**. |
    | **Execution type** | Select **In process**. |

4. On the **Conditions** tab, click **Add definition** and, in the drop-down list, click *Asset (M.Asset)*.
5. In the condition list, select *Final lifecycle status*, *current value*, *contains*, *all*, and *Approved*.

    ![Example of the trigger conditions.](https://doc.sitecore.com/api/markdown-image/ch/en/developers/cloud-dev/images/step_three_-_configure_the_trigger.png)

6. On the **Actions** tab, under **Post actions**, click **Add action** and select the action you created.
7. Click **Save and close**.
8. In the **Save** dialog, click **Activate** to activate the trigger.

    After the trigger is activated, a public link will be created for an asset immediately after it is approved.

    ![Example of a public link created automatically upon approval.](https://doc.sitecore.com/api/markdown-image/ch/en/developers/cloud-dev/images/step_four_-_public_links_are_created_automatically_upon_approval.png)