Skip to content

Instantly share code, notes, and snippets.

@ivakhnov
Last active July 1, 2020 15:13
Show Gist options
  • Select an option

  • Save ivakhnov/6e9c2b71c2b6043aa232b800ecc23e70 to your computer and use it in GitHub Desktop.

Select an option

Save ivakhnov/6e9c2b71c2b6043aa232b800ecc23e70 to your computer and use it in GitHub Desktop.
[ExternalComponent invoke Scripting] Whenever you need to invoke an action Script (which you don't need to register in an M.Action entity), you can reuse the following construction. #sitecorecontenthub, #contenthub, #scripting, #externalcomponent #externalcomponentandscripting
var scriptIdentifier = "8m_bVGGMxk-zBdlAMuQVWA"; // No M.Action entity needed, just use the script identifier directly
var jsonData = { AssetId: assetId };
$.rest.post(`/api/scripts/${scriptIdentifier}/execute`, jsonData)
.done(function () {
options.messageBoard.addMessage({
text: "The script was invoked successfully!", level: "success"
});
})
.fail(function (xhr) {
if (xhr.status == 403)
{
// This is just an example case when M throws a 403, in case the modification done INSIDE the script are causing this.
// This case might be not appicable and the scenario might not follow the 403 response possibility, just an example
options.messageBoard.addMessage({
text: "This exception might be thrown from M, just an example case here", level: "warning"
});
}
else
{
// Those exception messages are coming from the Script ITSELF, if there is an Exception thrown from inside.
for (var key in xhr.responseJSON){
options.messageBoard.addMessage({
text: xhr.responseJSON[key], level: "danger"
});
}
}
});
//// CSX (C# Script) code in the Script:
//// Then in the Scripting you can use the following construction to access the passed data:
// var assetIdString = Context.Data["AssetId"]?.ToString();
// if (string.IsNullOrEmpty(assetIdString))
// {
// throw new ValidationException(
// "When merging assets, the source Asset Id should not be null!",
// new ValidationFailure("Please, specify the id of the asset id which you want to merge into the other asset!", "AssetId"));
// }
// var sassetId = long.Parse(assetIdString);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment