02-19-2018 10:54 AM
Hello everyone,
I'm trying to execute some JavaScript code ( using Alfresco JavaScript API method ) from Java. Please if someone know how to do this.
Thanks in advance.
03-01-2018 12:22 PM
Yes, there are many ways to do it, but that is one. You are basically leveraging the out-of-the-box script action. This is the same as if you had created a folder rule and selected "Execute Script" as the action, and then specified a JavaScript file that is sitting in the Data Dictionary/Scripts folder.
02-19-2018 02:10 PM
Without more context the best I can do is suggest that you read the source. Specifically, take a look at
org/alfresco/repo/action/executer/ScriptActionExecuter.java to see how Alfresco leverages the ScriptService to run server side JavaScript such as when someone configures a folder rule to use "Execute Script".
Depending on what you want to do you might also pick up a few tips by looking at org/alfresco/repo/processor/ScriptServiceImpl.java.
02-20-2018 07:40 AM
The script action should be the preferred way to call a JavaScript source file sitting on your classpath or stored in a node within the Repository. That way you can be sure that you could use that very same file also for rules or user-triggered actions. ScriptService is the low-level API for invoking JavaScript code and - apart from my attempts of writing a Nashorn based engine or running non-persistent, user-provided JavaScript code - I have so far not found any good reason to use that over the script action in any project.
02-20-2018 12:02 PM
Mr. Jeff , Mr. Axel thank you for the reply and the precise answer.
I did took a look at those classes and did some research. I found this :
// Create the script node reference
NodeRef script = this.nodeService.createNode(
this.folder,
ContentModel.ASSOC_CONTAINS,
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "testScript.js"),
ContentModel.TYPE_CONTENT).getChildRef();
this.nodeService.setProperty(script, ContentModel.PROP_NAME, "testScript.js");
ContentWriter contentWriter = this.contentService.getWriter(script, ContentModel.PROP_CONTENT, true);
contentWriter.setMimetype("text/plain");
contentWriter.setEncoding("UTF-8");
contentWriter.putContent("\"VALUE\";");
// Create the action
Action action1 = this.actionService.createAction(ScriptActionExecuter.NAME);
action1.setParameterValue(ScriptActionExecuter.PARAM_SCRIPTREF, script);
// Execute the action
this.actionService.executeAction(action1, this.nodeRef);
Is it like that we can execute JavaScript from a Java class? If it is, how do I import "ScriptActionExecuter"
in my Java code that lets me executing the code above.
Sorry if I misunderstood something, I'm new to this field.
Thanks a lot.
03-01-2018 12:22 PM
Yes, there are many ways to do it, but that is one. You are basically leveraging the out-of-the-box script action. This is the same as if you had created a folder rule and selected "Execute Script" as the action, and then specified a JavaScript file that is sitting in the Data Dictionary/Scripts folder.
03-02-2018 03:31 AM
03-02-2018 03:18 PM
Have you tried using the Alfresco SDK?
https://ecmarchitect.com/alfresco-developer-series-tutorials/maven-sdk/tutorial/tutorial.html
03-02-2021 07:34 AM
Hey
Create a file with hello.js
Write and save the code in the file
Open terminal
Write command jjs hello.js and press enter
After procedure you will find output.
Explore our Alfresco products with the links below. Use labels to filter content by product module.