How to create a Shared script using Visual Studio Code
Shared scripts allow to maintain and reuse commonly used code in a separate script that can be referenced by one or more scripts of any type.
- .NET 3.1 SDK
- Visual Studio code
- Visual Studio code C# extension
- Sitecore Content Hub CLI, installation instruction here
Initialize your working folder
First initialize your working folder with the files needed to support Intellisense in Visual Studio Code. The working folder can be any folder selected by you.
ch-cli scripting initialize -f <your-folder-path>
Once initialized the working folder should contain the following files:
[working folder] ├── omnisharp.json ├── references.json ├── lib │ ├── Newtonsoft.Json.dll │ ├── Remotion.Linq.dll │ ├── Stylelabs.M.Scripting.Types.dll │ ├── Stylelabs.M.Sdk.dll │ ├── Stylelabs.M.Sdk.Fluent.dll │ ├── Stylelabs.M.Sdk.ScriptClient.dll │ └── Stylelabs.Scripting.dll ├── references │ ├── Action.csx │ ├── Base.csx │ ├── MediaProcessing.csx │ ├── Shared.csx │ ├── UserPostRegistration.csx │ ├── UserPreRegistration.csx │ ├── UserSignIn.csx
The files on the working folder might change depending on the Content Hub version of the instance you are connected to.
Create your Shared script
- Open your working folder in Visual Studio Code.
- Create a new file with extension
.csxat the root of the working folder. The name of the file will be the name of your script.
Open the file and add the following header:
#load "./references/Shared.csx" /**------------ Include above to support intellisense on Content Hub types in editor ----------------**/ // Script Start
The script's header will determine the script's type. All lines in the header are required.
Save the file. At this point, you should have Intellisense.
Start the script Watcher:
ch-cli scripting watch -f <your-folder-path>
You can run this command from Visual Studio Code's terminal.
At this point, your Shared script has been created on the Content Hub instance you are connected to.
Work on your script
Every time you save your script, the script Watcher will compile and publish it.
Enable your Shared script.
ch-cli scripting enable -n <your-script-name>
Reference your Shared script from other scripts
Reference your Shared script by its name, for example:
#load "./references/Action.csx" /**------------ Include above to support intellisense on Content Hub types in editor --------------**/ // Script Start #load "my-shared-script.csx" ...
Can we improve this article ? Provide feedback