I want to execute some server-side JavaScript (to manipulate repository nodes).
I have admin access via Alfresco Share, but I have no Alfresco Explorer nor SSH access.
- The best would be a kind of console in which I could type commands, like JavascriptConsole but it can't be installed without SSH access. I don't think there is anything like this, but maybe there are tricks involving rules?
- Writing one-time webscripts sounds overkill and inconvenient.
Is it possible?
(Alfresco 5.0)
If you don't have any access to the repository machine, I think your easiest option would be the Data Dictionary + dummy rule option
Firstly, create you javascript code on your local machine. Next, upload it into the Data Dictionary. Pick the Repository, then Data Dictionary, then Scripts, so a full path of
Repository > Data Dictionary > Scripts
. When you upload it, make sure the content / mime type is set toMimetype: Java Script
, changing it in Edit Properties if neededNow, create a dummy folder somewhere. On that folder, pick
Manage Rules
, create a new rule. Set it to run onItems are created or enter this folder
and perform action ofExecute Script
and pick your newly uploaded script as the one to runFinally, create a random file in your folder every time you want to run the script!
Edit: If your script has any problems, then these will only be logged on the repo side, which you won't be able to see. Probably your best bet therefore is to wrap the whole thing in a giant try/catch block, and have it log the exceptions into another file, in a non rules folder!
I think the one-time webscript is a better option than the rule approach. This can be deployed in Data Dictionary/Web Scripts Extensions with two files:
Descriptor (e.g., output-data.get.desc.xml):
Write the controller in JavaScript, outputting to a content node in the repository, and also as your last line putting the content node in the webscript model so it can be streamed to the browser (the purpose of the "kind" attribute):
Controller (e.g., output-data.get.js):
A quick "Refresh Web Scripts" (button at the bottom of http://localhost:8080/alfresco/s/index), and your webscript is now easily runnable. You can even change the JavaScript code and not have to Refresh the webscript again.
The issues with logging can be handled as in the other answer, by wrapping the meat of the code in a try/catch and writing error messages to the output file as well.