---
title: "Shared script referencing another script"
slug: "shared-script-referencing-another-script"
abstract: "Web script example of a shared script that references another script and exposes a method."
---
# Shared script referencing another script

This is an example of a *Shared* script that is referenced by another script and exposes a simple method.

A shared script can include other shared scripts. However, it is important to avoid creating cyclic references. In the event that cyclic references are found during the compilation, the build output will provide some details about the conflicting scripts. Use this when you want to reuse centralized script logic across multiple dependent script.

> [!note]
> **Before you begin**
> -   Ensure that all dependent scripts reference the shared script correctly.
>
> -   Define properties and configurations shared between the main and dependent scripts, such as entity IDs or relation names.

## Shared script

For this example, the name of the shared script is *My Shared Script*.

```csharp
public string GetExtension(string path)
{
  var tokens = path.Split('.');
  if (tokens.Length > 1)
  {
    return "." + tokens[tokens.Length - 1];
  }
  return null;
}

```

## Shared script explanation

This script consists of a method that retrieves the file extension from a given file path. It splits the path at each dot (`.`) and returns the last segment with a preceding dot (`.`) if an extension exists. If no extension is found, it returns `null`.

```csharp
public string GetExtension(string path)
{
  var tokens = path.Split('.');
  if (tokens.Length > 1)
  {
    return "." + tokens[tokens.Length - 1];
  }
  return null;
}

```

## Dependent script

The following example is partially derived from the [Action script example (Pre-commit phase)](https://doc.sitecore.com/ch/en/developers/cloud-dev/action-script-with-a-pre-commit-trigger.html "Action script with a pre-commit trigger"), but the approach would be the same for any other script type.

```csharp
#load "My Shared Script.csx"

var entity = Context.Target as IEntity;

// Make sure the following members are loaded
await entity.LoadMembersAsync(new PropertyLoadOption("FileName"), new RelationLoadOption("AssetTypeToAsset"));

var fileName = entity.GetPropertyValue<string>("FileName");
var extension = GetExtension(fileName);

// ...

```

## Dependent script explanation

Reference the shared script by its name. Multiple shared scripts can be referenced by adding multiple `#load "<Script Name>"` statements.

```csharp
#load "My Shared Script.csx"

```

> [!note]
> If you are using the IntelliSense support provided by the [CLI](https://doc.sitecore.com/ch/en/developers/cloud-dev/content-hub-cli.html "Content Hub CLI"), you can add the `.csx` extension to the script name. That allows files to be resolved when working with scripts in the local file system.

Retrieve the `Target` object from the `Context` and cast it to `IEntity`. `Target` is the asset involved in the creation or modification event.

```csharp
var entity = Context.Target as IEntity;

```

Make sure that the file name property and asset type relation are loaded. If they are not, they will be lazy-loaded.

```csharp
await entity.LoadMembersAsync(new PropertyLoadOption("FileName"), new RelationLoadOption("AssetTypeToAsset"));

```

Retrieve the `FileName` property from the `Target` object using `GetPropertyValue`.

```csharp
var filename = entity.GetPropertyValue<string>("FileName");

```

Call the `GetExtension(string path)` method, which is made available through the shared script.

```csharp
var extension = GetExtension(fileName);

```

## Scope

Although you can add members, properties, and so on, to the global scope of a script, it is better to group them into classes to allow better separation and easier maintenance. This example could be converted into the following:

```csharp
public static class FileHelper
{
  public static string GetExtension(string path)
  {
    var tokens = path.Split('.');
    if (tokens.Length > 1)
    {
      return "." + tokens[tokens.Length - 1];
    }
    return null;
  }
}

```

> [!note]
> The `GetExtension` method has been made static, otherwise a `FileHelper` instance would need to be created before calling the method.

The call to the method would look like this:

```text
var extension = FileHelper.GetExtension(fileName);

```