Create a shared script using Visual Studio Code

Shared scripts let you maintain and reuse commonly used code in a separate script that can be referenced by one or more other scripts of any type.

Before you begin
  • initialize your working folder

Note

The scripting command only runs on the script code and does not affect the entity property or relation. When you push a script to a new environment, a script entity is created. The action entity that references this script entity is not pushed to the new environment.

To create a shared script:

Note

Every time you save your script, the script watcher compiles and publishes it.

  1. In your working folder, create a file with the extension .csx at the root of the working folder.

  2. Open the file and add the following header:

    RequestResponse
    #load "./references/Shared.csx"
    /**------------ Include above to support intellisense on Content Hub types in editor ----------------**/
    // Script Start
    
    Important

    The script header is determined by the script type. All lines in the header are required.

  3. Save the file.

  4. In a terminal, run the following command to start the script watcher:

    RequestResponse
    ch-cli scripting watch -f <YOUR_FOLDER_PATH> 
    

    The shared script is created on the Content Hub instance you are connected to.

  5. Write the remainder of your script.

  6. Run the following command to enable your shared script:

    RequestResponse
    ch-cli scripting enable -n <YOUR_SCRIPT_NAME> 
    
  7. To reference your shared script from other scripts, you can refer to the script by name as shown in the following example:

    RequestResponse
    #load "./references/Action.csx"
    /**------------ Include above to support intellisense on Content Hub types in editor --------------**/
    // Script Start
    
    #load "<MY_SHARED_SCRIPT.CSX>"
    
    ...
    

Do you have some feedback for us?

If you have suggestions for improving this article,