0

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)

Nicolas Raoul
  • 1,334
  • 7
  • 22
  • 43

2 Answers2

1

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 to Mimetype: Java Script, changing it in Edit Properties if needed

Now, create a dummy folder somewhere. On that folder, pick Manage Rules, create a new rule. Set it to run on Items are created or enter this folder and perform action of Execute Script and pick your newly uploaded script as the one to run

Finally, 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!

Gagravarr
  • 737
  • 3
  • 7
  • 21
  • Nope, that'll get logged on the repo server. Your only option really is to wrap it in a giant try/catch block, and have it output the exception/error details caught there into a new node – Gagravarr May 27 '15 at 14:23
1

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):

<webscript kind="org.alfresco.repository.content.stream">
    <shortname>Output metadata</shortname>
    <description>Output data on content files</description>
    <url>/output-data.txt</url>
    <format default="">extension</format>
    <family>Eric</family>
    <authentication>user</authentication>
    <transaction>required</transaction>
</webscript>

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):

// just some text to output for demonstration purposes
var contentString = "Output";

// create a text document in company home
var outputDoc = companyhome.createNode('output-data.txt', 'cm:content');
outputDoc.content = contentString;
outputDoc.save();

//stream the document
model.contentNode = outputDoc;

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.

Eric
  • 11
  • 2