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.
Solved! Go to Solution.
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.
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.
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.
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.
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.
Have you tried using the Alfresco SDK?
https://ecmarchitect.com/alfresco-developer-series-tutorials/maven-sdk/tutorial/tutorial.html
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.
Ask for and offer help to other Alfresco Content Services Users and members of the Alfresco team.
Related links:
By using this site, you are agreeing to allow us to collect and use cookies as outlined in Alfresco’s Cookie Statement and Terms of Use (and you have a legitimate interest in Alfresco and our products, authorizing us to contact you in such methods). If you are not ok with these terms, please do not use this website.